How to Find All Occurrences of a Substring in a String Using Python?

Recently in a Python webinar, the was the topic of discussion was how to find all occurrences of a substring in a string using Python. After researching and discussing I found three effective ways to accomplish this task. In this tutorial, Let us learn all the methods with suitable examples and screenshots of executed example code.

Find All Occurrences of a Substring in a String Using Python

Python provides various ways to find all occurrences of a substring in a string. Let us learn all the important methods.

Read How to Fix Unterminated String Literals in Python?

Method 1. Use re.finditer() Function

The re.finditer() function in Python from the re module finds all occurrences of a substring, including overlapping matches.

Example: Find “New” in USA city names

import re

text = "New York, New Jersey, Newport, New Orleans"
pattern = r"New"  # Substring to search for

matches = [match.start() for match in re.finditer(pattern, text)]

print("Occurrences of 'New':", matches)

Output:

Occurrences of 'New': [0, 10, 22, 31]

In the screenshot below you can see the executed example code with the output.

Find All Occurrences of a Substring in a String Using Python

"New" appears at index 0 (New York), 10 (New Jersey), 22 (Newport), and 30 (New Orleans).re.finditer() returns iterators with match positions.

Check out How to Extract a Substring Between Two Characters in Python?

Method 2. Use str.find() Method

The str.find(substring, start) method in Python finds the first occurrence of a substring after start. We use a loop to find all occurrences.

Example: Find “San” in USA city names

text = "San Francisco, San Diego, Santa Monica, San Jose"
substring = "San"

positions = []
index = text.find(substring)  # Find first occurrence
while index != -1:
    positions.append(index)
    index = text.find(substring, index + 1)  # Find next occurrence

print("Occurrences of 'San':", positions)

Output:

Occurrences of 'San': [0, 15, 26, 40]

In the screenshot below you can see the executed example code with the output.

How to Find All Occurrences of a Substring in a String Using Python

"San" appears at index 0 (San Francisco), 15 (San Diego), 26 (Santa Monica), and 40 (San Jose).find() returns -1 when no more occurrences are found.

Read How to Sort a String in Python?

Method 3. Use List Comprehension with range()

This method searches through all possible start positions in the Python string and collects matches.

Example: Find “ton” in USA city names

text = "Houston, Boston, Charleston, Trenton"
substring = "ton"

matches = [i for i in range(len(text)) if text.startswith(substring, i)]

print("Occurrences of 'ton':", matches)

Output:

Occurrences of 'ton': [4, 12, 24, 33]

In the screenshot below you can see the executed example code with the output.

Find All Occurrences of a Substring in a String Using Python range()

"ton" appears at index 4 (Houston), 13 (Boston), 24 (Charleston), and 32 (Trenton).text.startswith(substring, i) checks if the substring starts at the index i.

Check out How to Use a Raw Strings in Python?

Conclusion

In this tutorial, I have explained how to find all occurrences of a substring in a string using Python. I have discussed some important methods such as using the re.finditer() function, str.find() method, and list comprehension.

You may read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.