A lot of computations involve processing a collection one item at a time. For strings this means that we would like to process one character at a time. Often we start at the beginning, select each character in turn, do something to it, and continue until the end. This pattern of processing is called a traversal.
We have previously seen that the for
statement can iterate over the items of a sequence (a list of names in the case below).
Recall that the loop variable takes on each value in the sequence of names. The body is performed once for each name. The same was true for the sequence of integers created by the range
function.
Since a string is simply a sequence of characters, the for
loop iterates over each character automatically.
The loop variable achar
is automatically reassigned each character in the string “Go Spot Go”. We will refer to this type of sequence iteration as iteration by item. Note that it is only possible to process the characters one at a time from left to right.
The index positions in “apple” are 0,1,2,3 and 4. This is exactly the same sequence of integers returned by range(5)
. The first time through the for loop, idx
will be 0 and the “a” will be printed. Then, idx
will be reassigned to 1 and “p” will be displayed. This will repeat for all the range values up to but not including 5. Since “e” has index 4, this will be exactly right to show all of the characters.
In order to make the iteration more general, we can use the len
function to provide the bound forrange
. This is a very common pattern for traversing any sequence by position. Make sure you understand why the range function behaves correctly when using len
of the string as its parameter value.
You may also note that iteration by position allows the programmer to control the direction of the traversal by changing the sequence of index values. Recall that we can create ranges that count down as well as up so the following code will print the characters from right to left.
Trace the values of idx
and satisfy yourself that they are correct. In particular, note the start and end of the range.
The loop condition is position < len(fruit)
, so when position
is equal to the length of the string, the condition is false, and the body of the loop is not executed. The last character accessed is the one with the index len(fruit)-1
, which is the last character in the string.