Replacement index 1 out of range for positional args tuple

avatar
Borislav Hadzhiev

Last updated: Apr 8, 2024
2 min

banner

# Replacement index 1 out of range for positional args tuple

The Python "IndexError: Replacement index 1 out of range for positional args tuple" occurs when we use the format() method without providing a value for all replacement fields.

To solve the error, make sure to provide a value for each replacement field.

indexerror replacement index 1 out of range

Here is an example of how the error occurs.

main.py
# โ›”๏ธ IndexError: Replacement index 1 out of range for positional args tuple my_str = 'His name is {} {}'.format('Bobby')

how the error occurs

Notice that we only passed a value for the first replacement field.

# Specify a value for each replacement field

To solve the error, make sure to specify a value for each replacement field.

main.py
my_str = 'His name is {} {}'.format('Bobby', 'Hadz') print(my_str) # ๐Ÿ‘‰๏ธ 'His name is Bobby Hadz'

specify value for each replacement field

The string in the example has 2 replacement fields {}, so we passed 2 arguments to the str.format() method.

If you only meant to have 1 replacement field, remove the second one.

main.py
my_str = 'His name is {}'.format('Bobby') print(my_str) # ๐Ÿ‘‰๏ธ His name is Bobby

The string in the example has only 1 replacement field, so we passed a single argument in the call to format().

# Unpacking a list or a tuple in the call to format()

If you have an iterable (e.g. a list or a tuple), use an asterisk to unpack the iterable's items.

main.py
my_list = ['Bobby', 'Hadz'] my_str = 'His name is {} {}'.format(*my_list) print(my_str) # ๐Ÿ‘‰๏ธ 'His name is Bobby Hadz'

unpacking list or tuple in call to format

The asterisk * unpacks the items of the iterable in the call to the format() method.

When using unpacking, you have to make sure the iterable has exactly as many elements as there are replacement fields {} in the string.

The str.format() method performs string formatting operations.

main.py
first = 'Bobby' last = 'Hadz' result = "His name is {0} {1}".format(first, last) print(result) # ๐Ÿ‘‰๏ธ "His name is Bobby Hadz"

The string the method is called on can contain replacement fields specified using curly braces {}.

# Using keyword arguments instead of positional ones

You can also use keyword arguments instead of positional ones.

main.py
first = 'Bobby' last = 'Hadz' result = "His name is {f} {l}".format(f=first, l=last) print(result) # ๐Ÿ‘‰๏ธ "His name is Bobby Hadz"

using keyword arguments instead of positional ones

Make sure to provide exactly as many arguments to the format() method as you have replacement fields in the string.

If you decide to use positional arguments with numeric indices, make sure to start from 0, e.g. {0} as indices are zero-based in Python.

# Using a formatted string literal instead of str.format()

Alternatively, you can use a formatted string literal to concatenate strings.

main.py
first = 'Bobby' last = 'Hadz' result = f'His name is {first} {last}' print(result) # ๐Ÿ‘‰๏ธ "His name is Bobby Hadz"
Formatted string literals (f-strings) let us include expressions inside of a string by prefixing the string with f.

Make sure to wrap expressions in curly braces - {expression}.

This should be your preferred approach as formatted string literals are a more recent addition to Python and offer a much easier-to-read syntax.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev