Menu
Coddy logo textTech

findall

Lesson 2 of 28 in Coddy's RegEx in Python course.

The re library provides several functions for working with regular expressions. To start using it, import the library:

import re

The findall function searches a string for all occurrences that match a specified pattern and returns them as a list. It's a great way to extract multiple matching substrings from a text.

re.findall(pattern, string)
  • pattern: The regular expression pattern you want to search for.
  • string: The string in which you want to search for the pattern.

For example:

import re

text = "I have 2 apples and 3 bananas."
pattern = r"\d"  # This pattern simply matches any digit

matches = re.findall(pattern, text)

print(matches)  # Output: ['2', '3']

In the above example, r"\d" is a simple regex pattern that matches any digit. The findall function finds all digits in the string text and returns them as a list.

challenge icon

Challenge

Easy

You are given a variable text that holds some string, use the r"\w+" pattern (a simple regex expression that matches sequences of word characters) to extract all the words in text, print the result at the end.

Try it yourself

import re

text = "Python is amazing! Let's learn regex."

# Write code here

All lessons in RegEx in Python