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)]
print(l)

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

To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And 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?

Practice projects is how you sharpen your saw in coding!

Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?

Then become 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!

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