LearnFrenzy
Sign in Take a free test

Strings · Find the Error · Question 1

Error: index equal to the length Answer, explanation & discussion

  • Find the error
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
Question 1 Back to Exercise

The program below stops with an error. Which statement correctly describes the problem?

main.py
word = "python"
print("Last letter:", word[len(word)])
  • A TypeError: len() returns a str
  • B IndexError: valid indexes are 0 to len(word) - 1; use word[-1] or word[len(word) - 1] Correct
  • C It prints Last letter: n
  • D SyntaxError: nested brackets
Correct Answer: Option B

A 6-character string has indexes 0–5. word[-1] is the idiomatic last character.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links