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. This pattern of processing is called a traversal. One way to write a traversal is with a while loop:
This loop traverses the string and displays each letter on a line by itself. The loop condition is index < len(fruit), so when index is equal to the length of the string, the condition is false, and the body of the loop doesn’t run. The last character accessed is the one with the index len(fruit)-1, which is the last character in the string.
As an exercise, write a function that takes a string as an argument and displays the letters backward, one per line.
Another way to write a traversal is with a for loop:
Each time through the loop, the next character in the string is assigned to the variable letter. The loop continues until no characters are left.
The following example shows how to use concatenation (string addition) and a for loop to generate an abecedarian series (that is, in alphabetical order). In Robert McCloskey’s book Make Way for Ducklings, the names of the ducklings are Jack, Kack, Lack, Mack, Nack, Ouack, Pack, and Quack. This loop outputs these names in order:
The output is:
Of course, that’s not quite right because “Ouack” and “Quack” are misspelled. As an exercise, modify the program to fix this error.
It is tempting to use the [] operator on the left side of an assignment, with the intention of changing a character in a string. For example:
The “object” in this case is the string and the “item” is the character you tried to assign. For now, an object is the same thing as a value.
The reason for the error is that strings are immutable, which means you can’t change an existing string. The best you can do is create a new string that is a variation on the original:
This example concatenates a new first letter onto a slice of greeting. It has no effect on the original string.
What does the following function do?
In a sense, find is the inverse of the [] operator. Instead of taking an index and extracting the corresponding character, it takes a character and finds the index where that character appears. If the character is not found, the function returns -1.
This is the first example we have seen of a return statement inside a loop. If word[index] == letter, the function breaks out of the loop and returns immediately.
If the character doesn’t appear in the string, the program exits the loop normally and returns -1.
This pattern of computation—traversing a sequence and returning when we find what we are looking for—is called a search.
As an exercise, modify find so that it has a third parameter, the index in word where it should start looking.
The following program counts the number of times the letter a appears in a string:
This program demonstrates another pattern of computation called a counter. The variable count is initialized to 0 and then incremented each time an a is found. When the loop exits, count contains the result—the total number of a’s.
As an exercise, encapsulate this code in a function named count, and generalize it so that it takes the string and the letter as arguments.
Then rewrite the function so that instead of traversing the string, it uses the three-parameter version of find from the previous section.