0

I have a tuple:

('ORF eins', '20:15', '21:05', 'soko-donau.html', 'Soko Donau', 'Schöne neue Welt')

that has six elements (index 0-5).

If I print with string formatting, like this:

print("""Entry {}
Title: {}
Station: {}
Start Time: {}
End Time: {}""".format(programID, details[4], details[0], details[1]), details[2])

I get an "IndexError: tuple index out of range" although I only use the index until 4 and have 6 elements in my tuple.

1
  • @Abstracted there's no more readable way to create a multi-line string. If reserving """ for docstrings was important you'd expect PEP 8 to say something about it, unless it's there and I missed it. Commented Jun 7, 2016 at 22:59

2 Answers 2

4

It looks like you have a parenthesis in the wrong place:

print("""Entry {}
Title: {}
Station: {}
Start Time: {}
End Time: {}""".format(programID, details[4], details[0], details[1]), details[2])
#                                                                   ^

So your format statement is getting 4 arguments when it is expecting 5 (because there are 5 "substitution slots {}") so when it tries to get the 5th parameter, it has an IndexError.

You'll get the same thing with "{}".format() for example:

>>> "{}".format()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: tuple index out of range
Sign up to request clarification or add additional context in comments.

1 Comment

saw it a few minutes after i posted it, works perfectly fine now of course :D thanks anyway!
1

You have a closing bracket after details[1] which messes up your code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.