Skip to the content

findall and groups

Python · Unit 18: Regular expressions

re.search hands you one match, the first one. re.findall hands you a list with every match.

If it finds nothing, the list comes back empty.

import re

t = "3 tacos and 2 waters"
print(re.findall(r"\d+", t))

Prints

['3', '2']

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 it so that it prints only 4821.

  5. 5. Predict the output

    What does this code print?

  6. 6. Multiple choice

    When is findall a better fit than search?

  7. 7. Complete the code

    Complete it so that it prints how many times today shows up.

  8. 8. Find the bug

    This program should print how many numbers the text has. Which line has the error?

  9. 9. Put the lines in order

    Put the function in order. It adds up every number in the text.

  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 →