In Python, we have methods like string.find()
and string.rfind()
to get the index of the first occurrence of a substring from the beginning or the end. However, I am wondering if there’s a method like string.find_all()
that can return all indexes where the substring is found, not just the first one from either direction.
For example:
string = "test test test test"
print(string.find('test')) # 0
print(string.rfind('test')) # 15
# This is the goal
print(string.find_all('test')) # [0, 5, 10, 15]
How can I achieve this Python find all occurrences in string?
Using a loop with string.find(), I’ve worked with Python for quite some time, and one of the simplest ways I know to find all substring occurrences is using string.find()
in a loop. Here’s a quick example:
def find_all_occurrences(string, substring):
start = 0
while start < len(string):
start = string.find(substring, start)
if start == -1:
break
yield start
start += len(substring)
string = "test test test test"
print(list(find_all_occurrences(string, 'test'))) # Output: [0, 5, 10, 15]
This method uses string.find()
to locate the next occurrence of the substring and keeps going until there are no more matches. It’s straightforward and works well for many cases.
Using re.finditer() from the re module
That’s a solid method, Rima! I’d like to add another approach for those who might be working with more complex patterns or need an efficient alternative—using regular expressions. Check this out:
import re
string = "test test test test"
matches = [match.start() for match in re.finditer('test', string)]
print(matches) # Output: [0, 5, 10, 15]
The re.finditer()
function is handy because it returns an iterator with match objects for all occurrences of the substring. This can be particularly useful if you want to do something more with each match—like capturing surrounding context or working with groups. Plus, it’s clean and Pythonic. If you’re already comfortable with regex, this is a great way to go!
Using List Comprehension with string.find()
Nice one, Joe! Here’s another angle for those who love concise, Pythonic solutions. You can actually achieve the same result in a single line using list comprehension:
string = "test test test test"
substring = "test"
matches = [i for i in range(len(string)) if string.startswith(substring, i)]
print(matches) # Output: [0, 5, 10, 15]
This approach is simple yet effective. It iterates through every possible starting index in the string and checks if the substring begins there using string.startswith()
. While it’s slightly less efficient for very large strings compared to other methods, it’s compact and clear—perfect for quick scripts or scenarios where readability is a priority.