Skip to the content

Looping over text

Python · Unit 6: Strings

A for goes through a string letter by letter, just like it goes through a list.

Spaces and punctuation get their own round too.

for letter in "sun":
    print(letter)

Prints

s
u
n

The rest of the explanation is in the lesson, which is part of the full plan.

Exercises in this lesson

You do them in the app, which checks them on the spot and explains why.

  1. 1. Predict the output

    What does this code print?

  2. 2. Predict the output

    What does this code print?

  3. 3. Predict the output

    What does this code print?

  4. 4. Complete the code

    Complete the function that counts the vowels in a string.

  5. 5. Find the case that fails

    count_a(text) should count how many a the text has, uppercase or lowercase. Which call breaks it?

  6. 6. Predict the output

    What does this code print?

  7. 7. Find the bug

    This program should remove the spaces and print "helloworld". Which line has the error?

  8. 8. Multiple choice

    What does this function do?

  9. 9. Put the lines in order

    Order the function that hides the vowels with *.

  10. 10. Predict the output

    What does this code print?

Do this lesson

It opens in your browser. This lesson is part of the full plan; the first unit of each course is free.

See all lessons →