Course outline · 0% complete

0/27 lessons0%

Course overview →

Looping over text

lesson 5-4 · ~11 min · 18/27

Strings are sequences too

A lot of everyday engineering is examining text one character at a time: a password checker counting digits, a form validator hunting for illegal characters, a word game scoring letters. Python makes this direct, because a for loop can walk any sequence, and a string is a sequence of its characters.

for letter in "code":
    print(letter)

This runs the body four times, with letter holding "c", then "o", then "d", then "e". It is the same for loop from lesson 5-1, and the only change is looping over a string's characters instead of range's numbers.

Notice that no counting is involved. There is no index, no length, and no range, which makes this the shortest correct way to visit every character and removes any chance of an off-by-one mistake.

One more built-in instruction earns its keep here. len(...) takes a string and gives back how many characters it contains, as a number, so len("banana") is 6. It works on other sequences too, as you will see in the next course.

Walking a string and measuring one

The loop visits each character in order, and len reports a count.

for letter in "code":
    print(letter)
print(len("banana"))

Output

c
o
d
e
6

The loop body runs once per character, producing four lines, and then len("banana") counts the six characters of a different word.

len counts characters rather than letters, so spaces and punctuation count too. len("hi there") is 8, and the space is one of them, which matters the first time you check whether a typed answer is long enough.

Counting characters with the accumulator

Combine the string loop with lesson 5-3's accumulator and lesson 4-2's if, and you can answer questions about any text. How many times do a or i appear in "programming"?

count = 0
for letter in "programming":
    if letter == "a" or letter == "i":
        count = count + 1
print(count)

Trace it: the letters are p-r-o-g-r-a-m-m-i-n-g, the condition is True at a and at i, so count ends at 2. This start-check-update shape is precisely how a real password validator counts digits or special characters before accepting a signup.

Looping over a two-character string

for letter in "hi":
    print(letter)

This prints h then i, on two separate lines.

A string is a sequence of characters, so the loop body runs once per character. First letter holds "h", then it holds "i", and each print call produces its own line exactly as in lesson 2-1.

The number of output lines always matches the number of characters, which is a quick way to sanity-check this kind of loop. Two characters in, two lines out.

Counting the letter s in mississippi

The accumulator counts only the characters that pass the condition.

count = 0
for letter in "mississippi":
    if letter == "s":
        count = count + 1
print(count)

Output

4

Reading the pieces

  • The condition letter == "s" sits inside the loop, indented once, and the count update is indented twice because it belongs to the if.
  • The final print is unindented so it runs once after the loop finishes. Indented, it would print a running count eleven times.
  • Spelling out m-i-s-s-i-s-s-i-p-p-i confirms four s characters, in two pairs.
  • The comparison is case-sensitive, so a capital S would not be counted. Counting both would mean letter == "s" or letter == "S", using the or from lesson 4-3.