Python RegEx findall() Functions

It is used to search for “all” occurrences that match a given pattern. In contrast, search() module will only return the first occurrence that matches the specified pattern. findall() will iterate over all the lines of the file and will return all non-overlapping matches of pattern in a single step.

Return all non-overlapping matches of pattern in string, as a list of strings. The function returns all the findings as a list. The search happens from left to right. If no matches are found, an empty list is returned.

Syntax:

re.findall(pattern, string, flags=0)

Parameter:

  • pattern:It is used to find the string.
  • string: In which the pattern has to be found.
  • flags:It is optional.

Return Value: It returns a List object.

Example:

import re

str = "Hello! Welcome to Python Programming"
res = re.findall("to",str)
print(res)

Output: ['to']

Tags