Reputation: 19
I cannot seem to understand why my code is displaying this error.
IndexError: tuple index out of range
Code:
l = ['Simpson', ',', 'Bartholomew', 'Homer', 'G400', 'Year', '2']
x = '{}'* len(l)
print(x)
x.format(l)
print(x)
Upvotes: 1
Views: 4561
Reputation: 1125018
You are passing in just one argument, the list l
, while your format string expects there to be 7 arguments.
If you wanted each element in l
to be formatted, then use the *arg
call syntax:
x.format(*l)
You want to print the return value, though:
result = x.format(*l)
print(result)
Demo:
>>> print(x.format(*l))
Simpson,BartholomewHomerG400Year2
Upvotes: 0
Reputation: 363556
Maybe you were looking for an unpacking:
>>> x.format(*l)
'Simpson,BartholomewHomerG400Year2'
Upvotes: 1