Python | List All Occurrences of Pattern in String

๐Ÿ’ฌ Question: Which method finds the list of all occurrences of the pattern in the given string?

Problem Formulation

Problem Formulation: Given a longer string and a shorter string. How to find all occurrences of the shorter string in the longer one?

Consider the following example:

  • Longer string: 'Finxters learn Python with Finxter'
  • Shorter string pattern: 'Finxter'
  • Result 1: ['Finxter', 'Finxter']

Optionally, you may also want to get the positions where the shorter string arise in the longer string:

  • Result 2: [(0, 'Finxter'), (27, 'Finxter')]

Method 1: Regex re.finditer()

To get all occurrences of a pattern in a given string, you can use the regular expression method re.finditer(pattern, string). The result is an iterable of match objects—you can retrieve the indices of the match using the match.start() and match.end() functions.

import re
s = 'Finxters learn Python with Finxter'
pattern = 'Finxter'

# Method 1: re.finditer
for m in re.finditer(pattern, s):
    print(pattern, 'matched from position', m.start(), 'to', m.end())

The output is:

Finxter matched from position 0 to 7
Finxter matched from position 27 to 34

๐ŸŒ Related Tutorial: Python Regex Finditer

Method 2: re.finditer() + List Comprehension

To get the pattern string, start index, and end index of the match into a list of tuples, you can use the following one-liner based on list comprehension:

[(pattern, m.start(), m.end()) for m in re.finditer(pattern, s)].

import re
s = 'Finxters learn Python with Finxter'
pattern = 'Finxter'

# Method 2: re.finditer + list comprehension
l = [(pattern, m.start(), m.end()) for m in re.finditer(pattern, s)]
print(l)

The output is:

[('Finxter', 0, 7), ('Finxter', 27, 34)]

๐ŸŒ Related Tutorial: Python List Comprehension

Method 3: Python String startswith()

The Python startswith(prefix, start) method checks whether a given string starts with a prefix when starting to search for the prefix at the index start.

We can use the startswith() method in a list comprehension statement to find all occurrences (positions) of a substring in a given string like so:

[i for i in range(len(s)) if s.startswith(pattern, i)]

Here’s the full example using this approach:

s = 'Finxters learn Python with Finxter'
pattern = 'Finxter'

# Method 4: startswith() to find all occurrences of substring in string
l = [i for i in range(len(s)) if s.startswith(pattern, i)]

print(l)

The output shows a list of start indices where the substring (pattern) was found in the original string:

[0, 27]

It pays to learn the basics in Python—feel free to dive deeper into this method in the following Finxter blog tutorial only one click away:

๐ŸŒ Related Tutorial: Python String Startswith

Method 4: re.findall()

If you’re interested in only the matched substrings without their index location in the given string, you can use the following approach.

To find all substrings in a given string, use the re.findall(substring, string) function that returns a list of matching substrings—one per match.

import re
s = 'Finxters learn Python with Finxter'
pattern = 'Finxter'

# Method 4: re.findall() to find all patterns in string
l = re.findall(pattern, s)
print(l)
# ['Finxter', 'Finxter']

In case you wonder how the regex.findall() method works, have a look at this graphic:

๐ŸŒ Related Tutorial: Python Regex Findall

Method 5: No-Regex, Recursive, Overlapping

The following method is based on recursion and it doesn’t require any external library.

The idea is to repeatedly find the next occurrence of the substring pattern in the string and call the same method recursively on a shorter string—moving the start position to the right until no match is found anymore.

All found substring matches are accumulated in a variable acc as you go through the recursion calls.

s = 'Finxters learn Python with Finxter'
pattern = 'Finxter'

# Method 5: recursive, without regex
def find_all(pattern, # string pattern
             string, # string to be searched
             start=0, # ignore everything before start
             acc=[]): # All occurrences of string pattern in string

    # Find next occurrence of pattern in string
    i = string.find(pattern, start)
    
    if i == -1:
        # Pattern not found in remaining string
        return acc
    
    return find_all(pattern, string, start = i+1,
                    acc = acc + [(pattern, i)]) # Pass new list with found pattern

l = find_all(pattern, s)
print(l)

The output is:

[('Finxter', 0), ('Finxter', 27)]

Note that this method also finds overlapping matches—in contrast to the regex methods that consume all partially matched substrings.

Where to Go From Here?

Enough theory. Letโ€™s get some practice!

Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation.

To become more successful in coding, solve more real problems for real people. Thatโ€™s how you polish the skills you really need in practice. After all, whatโ€™s the use of learning theory that nobody ever needs?

You build high-value coding skills by working on practical coding projects!

Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?

๐Ÿš€ If your answer is YES!, consider becoming a Python freelance developer! Itโ€™s the best way of approaching the task of improving your Python skillsโ€”even if you are a complete beginner.

If you just want to learn about the freelancing opportunity, feel free to watch my free webinar โ€œHow to Build Your High-Income Skill Pythonโ€ and learn how I grew my coding business online and how you can, tooโ€”from the comfort of your own home.

Join the free webinar now!

Resources: https://stackoverflow.com/questions/3873361/finding-multiple-occurrences-of-a-string-within-a-string-in-python