Iterating over strings
Iterating over strings
We’ve seen how we can iterate over sequences such as lists, tuples, and ranges. Python allows us to iterate over strings the same way we do for other sequences!
When we iterate over a string, the iterator returns one character at a time. Here’s an example:
>>> word = "cat"
>>> for letter in word:
... print(letter)
...
c
a
t
Copyright © 2023–2025 Clayton Cafiero
No generative AI was used in producing this material. This was written the old-fashioned way.