While Loops and Manual Traversal
The loop continues until all characters in the string have been visited. You do not need to manually check when to stop.
A Character-by-Character Journey
A string can be visited one character at a time. With a for loop, Python automatically assigns each character to a loop variable, one character per iteration. You focus on the work performed for each character instead of manually deciding which position to read next.
The loop continues until all characters in the string have been visited. You do not need to manually check when to stop.
Following the Loop Variable
Consider the string banana. It contains six characters. During traversal, the loop variable receives b first, then a, then n, then a, then n, and finally a. The loop variable holds one character at a time; it does not display the index number.
The repeated a and n values are not accidental repetitions by the loop. They occur because those characters appear more than once in banana. Each occurrence is visited in its own position.
Automatic Character Assignment
In a string-traversing for loop, you choose a variable to hold the current character. The variable is often named char or letter, but it can have another name. At each iteration, Python reassigns that variable to the next character. The keyword in identifies the string being traversed, and the indented loop body runs for each character.
In this pattern, char is the loop variable, fruit is the string being traversed, and print(char) is the loop body. The body runs once for each character. The loop variable therefore gives the body direct access to the current character without requiring you to write an index number.
Tracing Each Iteration
Tracing banana
Determine the value of char during each pass through the string banana.
- 1First pass
The loop variable receives b. The loop body processes b.
- 2Second pass
The loop variable is reassigned to a. The loop body processes a.
- 3Third pass
The loop variable is reassigned to n. The loop body processes n.
- 4Remaining passes
The loop continues with a, n, and a, processing each character in order.
The loop visits b, a, n, a, n, and a, then stops after all six characters have been visited.
Tracing means writing down the loop variable at every iteration and noting what the loop body does with it. This makes the progression visible. It also gives you a way to compare the expected character sequence with the behavior you observe.
What do you think happens?
If a for loop traverses banana and prints the loop variable once per iteration, what character sequence should it process?
Reveal answer
b, a, n, a, n, a
The loop visits every character in order. Repeated letters are visited because they occur at different positions in the string.
Manual Position Tracking
A while loop can also traverse a string, but this approach uses manual index tracking. The traversal must read a character at the current position, update the position, and check whether the end has been reached. These are responsibilities that a for loop handles automatically.
| For loop | While loop with manual traversal |
|---|---|
| Receives each character directly | Uses a manually tracked position to read characters |
| Handles advancing through the string | Requires the position to be updated |
| Stops after all characters are visited | Requires an end check |
| Usually simpler and more readable | Provides more manual control but more responsibilities |
Both approaches can traverse a string, but the for loop hides the index management that a while loop requires.
Finding a Divergence
When a string traversal does not produce the expected result, trace it instead of guessing. Write down the expected character sequence, then record the loop variable or manually tracked position at each iteration. The first difference identifies where the observed behavior diverges from the expected traversal.
Common mistake Expecting the loop variable to contain an index number
What it looks likeTreating char as a position instead of the current character
Why it failsIn a for loop over a string, the loop variable receives each character directly.
Do this insteadRead the loop variable as the current character and use tracing to record its value at each iteration.
Common mistake Assuming repeated characters mean the loop repeated an iteration
What it looks likeSeeing a, n, a, n, a while traversing banana
Why it failsThe string itself contains repeated characters at different positions.
Do this insteadDistinguish a repeated character value from a repeated traversal step.
Common mistake Forgetting that the loop body runs once per character
What it looks likeExpecting the body to run only once for a string
Why it failsThe body executes for each character visited by the loop.
Do this insteadTrace every assignment to the loop variable and every corresponding body execution.
Common mistake Managing a while-loop position incompletely
What it looks likeFailing to update the index or check whether the string end has been reached
Why it failsManual traversal requires those responsibilities.
Do this insteadTrack the current position, read the character, update the position, and perform the end check.
Practice and Prediction
Trace a for loop that traverses the string "level". Write the value of the loop variable in order for every iteration, then identify how many times the loop body runs.
Hints
- Write the characters from left to right.
- Record one loop-variable value for each character.
- The loop body runs once for every character visited.
A trace is expected to visit the characters b, a, n, a, n, a. The observed trace is b, a, a, n, a. Identify the first iteration at which the trace differs, and explain why tracing the loop variable is useful for debugging.
Hints
- Compare the expected and observed values from the first iteration onward.
- The first difference is more useful than only noticing that the final output is wrong.
- A trace records what the loop variable holds at each step.
Key Takeaways
- 1A for loop traverses a string by assigning each character to a loop variable, one character per iteration.
- 2The loop variable contains the current character, not the index number.
- 3Tracing each iteration makes the character sequence and loop-body execution visible.
- 4A while loop can perform manual traversal, but it requires explicit position management and an end check.
- 5For loops are generally simpler, more readable, and less error-prone for visiting every character in a string.
At a glance
- A for loop automatically assigns each character in a string to its loop variable.
- Tracing the loop variable reveals exactly what happens on every iteration.
- Repeated character values reflect repeated characters in the string, not necessarily repeated loop steps.
- A while loop requires manual index tracking, character reading, position updates, and stopping checks.
- Use a for loop when the goal is straightforward character-by-character traversal.