How to Find Multiple Occurrences of a String Within a String in Python?

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: 'Finxter'
  • Result: ['Finxter', 'Finxter']

Optionally, you may also want to print 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

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)]

The output is:

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

Method 3: 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 3: 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)

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.

Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!