9.10. Traversal and the for
Loop: By Item¶
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).
for aname in ["Joe", "Amy", "Brad", "Angelina", "Zuki", "Thandi", "Paris"]:
invitation = "Hi " + aname + ". Please come to my party on Saturday!"
print(invitation)
(ch08_4)
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.
for avalue in range(10):
print(avalue)
(ch08_5)
Since a string is simply a sequence of characters, the for
loop iterates over each character automatically.
for achar in "Go Spot Go":
print(achar)
(ch08_6)
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.
Check your understanding
strings-10-1: How many times is the word HELLO printed by the following statements?
s = "python rocks"
for ch in s:
print("HELLO")
strings-10-2: How many times is the word HELLO printed by the following statements?
s = "python rocks"
for ch in s[3:8]:
print("HELLO")
for
Loop: By Index