findall and groups
Python · Unit 18: Regular expressions
- Full plan
- Python
- 10 exercises
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. Predict the output
What does this code print?
2. Predict the output
What does this code print?
3. Predict the output
What does this code print?
4. Complete the code
Complete it so that it prints only 4821.
5. Predict the output
What does this code print?
6. Multiple choice
When is
findalla better fit thansearch?7. Complete the code
Complete it so that it prints how many times today shows up.
8. Find the bug
This program should print how many numbers the text has. Which line has the error?
9. Put the lines in order
Put the function in order. It adds up every number in the text.
10. Predict the output
What does this code print?
It opens in your browser. This lesson is part of the full plan; the first unit of each course is free.