Traversal and the for Loop: By Index

It is also possible to iterate through the indexes of a string or sequence. The for loop can then be used to iterate over these positions. These positions can be used together with the indexing operator to access the individual characters in the string.

Conveniently, we can use the range function to automatically generate the indices of the characters. range returns a special type (called an iterator, which the book will go into more detail about in another chapter) that allows you to iterate over numbers in sequence.

Consider the following codelens example.

(ch08_7)

The index positions in “apple” are 0,1,2,3 and 4. This is exactly the same sequence of integers you’ll get with 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.

Note that because range does not return a list (even though, in the textbook, it looks like it does), you should never write code like range(4)[2] in Python 3, because it won’t work. (It WILL work in this textbook, because of an oddity about how it works, but it will NOT work in normal Python 3.) However, using range for iteration, like in the example above, is just fine.

In order to make the iteration more general, we can use the len function to provide the bound for range. 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.

This example, which uses a specific list, rather than a result from the range function, works in a similar way:

(ch08_8)

Check your understanding

    exceptions-1: How many times is the letter p printed by the following statements?

    s = "python"
    for idx in range(len(s)):
       print(s[idx % 2])
    
  • 0
  • idx % 2 is 0 whenever idx is even
  • 1
  • idx % 2 is 0 whenever idx is even
  • 2
  • idx % 2 is 0 whenever idx is even
  • 3
  • idx % 2 is 0 whenever idx is even
  • 6
  • idx % 2 is 0 whenever idx is even
Next Section - Lists and for loops