Traversal with a for loop
A lot of computations involve processing a string one character at a time. Often they start at the beginning, select each character in turn, do something to it, and continue until the end. The pattern of processing is called traversal. One way to write a traversal is with a while statement:
index = 0 while index < len(fruit): letter = fruit[index] print letter index = index + 1
This loop traverses the string and displays each letter one a line by itself.
Another way to write a traversal is with a for loop:
for char in fruit: print char
Each time through the loop, the next character in the string is assigned to the variable char. The loop continues until no characters are left.
The following example shows how to use concatenation (string addition) and a for loop generate an abecedarian series (that is, in alphabetical order).
from Thinking in Python