Last modified: September 11, 2021

How to solve IndexError: Replacement index 1 out of range for positional args tuple format()

IndexError: Replacement index 1 out of range for positional args tupleappears when the placeholders are more than arguments.

Let's see an example.


lang = "python"

print("{} {}".format(lang))

Output:

Traceback (most recent call last):
  File "test.py", line 15, in <module>
    print("{} {}".format(lang))
IndexError: Replacement index 1 out of range for positional args tuple

As you can see, we've 2 placeholders {} and one format argument.

So how to solve this issue:

Solution 1: add the argument or delete the placeholder

add the argument:


lang = "python"
fram = "django"

print("{} {}".format(lang, fram))

Output:

python django

delete the placeholder:


lang = "python"

print("{}".format(lang))

Output:

python

Solution 2: Same value in all placeholders

If you want to insert the same value for all placeholders, follow this code:


lang = "python"

print("{0} {0}".format(lang))

Output:

python python