Strings: Indexing & Length
beginner10 min readLesson 9 of 169
Characters by position, zero-based indexing, and immutability.
A string is a sequence of characters. Each position has an index โ and Python counts from zero:
word = "Python"
word[0] # 'P' (first!)
word[5] # 'n' (last)
word[6] # IndexError โ only 0..5 exist
Negative indices count from the end: word[-1] is 'n', word[-2] is 'o'.
Length
len() counts characters: len("Python") is 6.
Strings are immutable
You cannot change a character in place: word[0] = "J" raises TypeError.
Instead, build a new string โ the next lesson shows slicing, and methods
give you more tools.