runtime
21 / 250
lesson 21 Loops & Iteration · beginner

Repeat for each character

for runs the same lines once for every character of a text, and a name kept outside the loop collects the result.

You'll be able to
  • Write for name in text: and indent the lines that repeat
  • Keep a count, a total or a piece of text that grows on every pass
  • Set the starting value before the loop and read the result after it
The idea

A phone number, a confirmation code and a film title are all text, and text is a row of characters. Plenty of everyday jobs mean looking at each character: count the digits in a phone number, add up the digits of a parcel number, hide the digits of a card number.

Writing one print per character only works for a text you know in advance. for repeats an indented block once for every character, however long the text is.

for ch in code: reads: take the characters of code one at a time, store the current one in ch, and run the indented lines. The line ends with a colon, and the lines that repeat are indented under it, just like under an if. The next line back at the left edge runs once, after the last pass.

Run each example to see what it prints. Running examples doesn't count toward progress.

example-1.py · Read a code out, one character per line
code = "K7Q2"
for ch in code:
    print("Character:", ch)
print("End of code")

The loop becomes useful when a name outside it keeps a result from pass to pass. This is called an accumulator. Give it a starting value before the loop, update it inside, and read it after. digits = digits + 1 reads the current count and stores one more.

If the starting line is missing, the very first pass tries to read a name that holds nothing yet:

example-2.py · Counting with no starting value
phone = "555-0132"
for ch in phone:
    if ch in "0123456789":
        digits = digits + 1
print(digits)
example-3.py · Count the digits in a typed phone number
phone = "(415) 555-0132"
digits = 0
for ch in phone:
    if ch in "0123456789":
        digits = digits + 1
print("Digits:", digits)

When to use it: whenever the answer depends on every character: how many, what total, or a new text built from the old one. If you only need to know whether some piece is in the text, in alone answers that without a loop.

Mistakes to avoid:

  • Putting the starting value inside the loop. Then it is reset on every pass, and only the last character counts.
  • Indenting the final print. Then it runs on every pass and prints a running total instead of one answer.
  • Forgetting the colon at the end of the for line, which is a syntax error.
Review
  • for ch in text: runs the indented lines once per character, with ch holding the current one.
  • An accumulator starts before the loop, changes inside it, and is read after it.
  • A reset inside the loop or an indented final print are the usual mistakes. Next: while, for repeats that don't have a fixed count.
Practise this

Loading your progress…

To master this lesson, solve both challenges without hints or the answer, and pass the concept check.

Repeat for each character — Loops & Iteration · Runtime