For Loops and String Traversal
Traversal is the process of handling a string one character at a time, from beginning to end.
One Character at a Time
Traversal is the process of handling a string one character at a time, from beginning to end. Instead of treating the string as one undivided value, a traversal moves through its positions in order and processes the character found at each position.
A while loop provides a clear way to express this pattern. The loop keeps track of an index, uses that index to access one character, and then moves the index forward. The same structure can be used whenever a task needs to examine every character in sequence.
Indexing the String
String positions begin at index 0. For the string 'banana', index 0 gives 'b', index 1 gives 'a', and the remaining characters follow at later indices. Because 'banana' has length 6, its valid indices are 0, 1, 2, 3, 4, and 5.
The final character is at index len(string) - 1. For 'banana', that final valid index is 5, because the string length is 6.
The Three-Part Loop
A traversal while loop has three essential pieces. First, the index starts at 0 so the loop begins with the first character. Second, the condition checks whether the index is still less than the string's length. Third, the index increases by 1 after the current character is accessed. Together, these pieces visit every character once, in order, and stop at the end.
fruit = 'banana' index = 0 while index < len(fruit): print(fruit[index]) index += 1
Tracing Banana
What do you think happens?
What will this traversal print, and in what order?
Reveal answer
Answer: It prints b, a, n, a, n, and a on separate lines, in that order.
The index starts at 0 and advances by 1 after each character is accessed. The valid indices for 'banana' are 0 through 5, so the characters are visited from beginning to end.
b
a
n
a
n
a| Index before iteration | Character accessed | Output | Index after increment |
|---|---|---|---|
| 0 | b | b | 1 |
| 1 | a | a | 2 |
| 2 | n | n | 3 |
| 3 | a | a | 4 |
| 4 | n | n | 5 |
| 5 | a | a | 6 |
Trace of the traversal through 'banana'. After index becomes 6, index < len(fruit) is false because the length is 6.
Boundary Mistakes
Using index <= len(fruit) as the loop condition.
When index reaches the string's length, the condition is still true. The loop then tries to access fruit[index], but that index does not exist.
Fix:
Use index < len(fruit). The last valid index is len(fruit) - 1.Starting the index at 1.
The traversal does not begin at the first character, whose index is 0.
Fix:
Start with index = 0 when the goal is to visit every character from the beginning.Forgetting to increment the index.
The index does not move to the next position, so the traversal cannot progress through the string.
Fix:
Increment the index after processing the current character with index += 1.
When debugging a traversal, write down the index before each iteration. Check three questions in order: Is the index at the intended starting position? Is the condition true for the current position? Will the increment move to the next position or past the boundary?
Trace It Yourself
Trace the loop below. Record the character accessed at each index and the index value when the loop stops.
Hints
- The string has length 6.
- The first index is 0.
- The loop condition is false when index reaches 6.
Checking the Final Iteration
What happens when the index is 5, and what happens after the increment?
Access: Index 5 identifies the final character of 'banana', which is 'a'.
Increment: After the character is printed, index increases from 5 to 6.
Condition check: The string length is 6, so index < len(fruit) becomes 6 < 6, which is false.
The final character is visited, and then the loop stops without attempting to access index 6.
Traversal Checklist
- Traversal handles a string one character at a time from beginning to end.
- A while-loop traversal starts its index at 0, accesses the character at that index, and increments the index by 1.
- The condition index < len(fruit) allows valid indices and stops when the index reaches the string length.
- For a string of length 6, the valid indices are 0 through 5, and the final character is at index len(fruit) - 1.
- To debug a traversal, trace the index, the accessed character, the condition, and the increment at every iteration.
Key Takeaways
- Traversal means processing a string one character at a time in order.
- The standard while-loop pattern uses an index that starts at 0, a bounds check using index < len(string), and an increment of 1.
- The final valid index is len(string) - 1; the index equal to the string length is the stopping point, not a character position.
- Off-by-one errors commonly come from using <= instead of <, starting at the wrong index, or failing to increment.