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.

"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.

"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.

"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:
- How to Return a String in Python?
- How to Convert a String to a Timestamp in Python?
- How to Remove Prefixes from Strings in Python?

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.