Back to questions
JuniorTheoryCommon
What does the program print?
word = "python"
print(word[-1], word[-6], len(word))n p 6
- Expecting an
IndexErrorforword[-6]: negative indices run from-1down to-len(word), so-6is the first character of a 6-letter word. - Expecting
5forlen(word), as if it gave the last index:lencounts the characters,6, while the last index is5, which is-1.
- What does
word[-7]do? - How would you print the second-to-last character of any string?