Iterating Through Strings with Loops
The first position in a Python string has index 0, not index 1.
One Character at a Time
A string is a sequence of individual characters. Because the characters are ordered, you can access one character at a time by referring to its position in the string. When you work through those positions in order, you are iterating through the string.
The first position in a Python string has index 0, not index 1.
Mapping Positions to Indices
Consider the generated example string CODE. Its characters appear in order, and each character is associated with an integer index. The index is an offset from the start of the string: the first character is zero positions away from the start, the second character is one position away, and so on.
| Index | Character |
|---|---|
| 0 | C |
| 1 | O |
| 2 | D |
| 3 | E |
The generated string CODE mapped from zero-based indices to characters.
Selecting a Character
The bracket operator selects a character from a string. The general form is string_name[index]. The value inside the brackets must be an integer, such as 0, 1, or 2. The integer identifies the character's index.
Extracting One Character
Use an index to select the character O from the generated string CODE.
- 1Identify the index
In the zero-based mapping, O is associated with index 1.
- 2Place the index in brackets
Use the string name followed by [1] to select the character at index 1.
- 3Read the selected character
The expression selects O from CODE.
The character at index 1 is O.
C
DFollowing the Loop
A loop can move through a string one character at a time. In the generated trace below, the loop begins with the character at index 0, then proceeds to the next character at index 1, and continues in order. Each step gives the loop one character from the string sequence.
word = "CODE"
for character in word:
print(character)- 1The first iteration uses C, the first character in the sequence.
- 2The next iteration uses O.
- 3The next iteration uses D.
- 4The final iteration uses E.
- word stores the generated string CODE.
- The loop takes one character from the string for each iteration.
- print displays the current character.
C
O
D
EIndexing and looping are related but different actions: bracket notation selects a character by a specific integer index, while a loop can visit the characters in sequence.
Fixing the First-Character Error
The most common indexing mistake is treating index 1 as the first character. Python counts the first character at index 0 because an index represents an offset from the beginning. Therefore, string[0] selects the first character, while string[1] selects the next character.
Common mistake Using index 1 when you want the first character
What it looks likeword = "CODE" followed by word[1]Why it failsIndex 1 identifies the second character, O, because the first character is at index 0.
Do this insteadUse word[0] when you want the first character, C.
Common mistake Using a non-integer index
What it looks likeword[1.0]Why it failsThe index inside the brackets must be an integer.
Do this insteadUse an integer index such as word[1].
word = "CODE"
print(word[1])The expression selects O, the second character.
word = "CODE"
print(word[0])The expression selects C, the first character.
Try the Trace
For the generated string "LOOP", identify the character at index 0, the character at index 1, and the character at index 3. Then write the characters in the order a loop would visit them.
Hints
- Start counting at index 0.
- Match each index with one character in the string.
- A loop moving through the sequence visits the characters in their string order.
Practice Solution
Trace the generated string LOOP using zero-based indices.
- 1Index 0
The character at index 0 is L.
- 2Index 1
The character at index 1 is O.
- 3Index 3
The character at index 3 is P.
- 4Loop order
Moving through the sequence from the beginning visits L, then O, then O, then P.
Index 0 is L, index 1 is O, index 3 is P, and the loop order is L, O, O, P.
Key Takeaways
- 1A string is an ordered sequence of individual characters.
- 2Python uses zero-based indexing, so the first character is at index 0.
- 3The bracket operator uses the form
string_name[index] to select one character. - 4The index must be an integer; a non-integer index raises a
TypeError. - 5A loop can visit the characters in a string one at a time and in sequence.
At a glance
- Strings contain individual characters arranged in sequence.
- The first character has index 0 because indices count offsets from the start.
- Use an integer inside brackets to extract a particular character.
- A loop can move through the characters one at a time.
- When correcting an indexing mistake, replace the assumed first-character index 1 with index 0.