Finding All Occurrences of a Substring in a String (All Methods Included) in python

What is a string in python?

In Python, a string is a sequence of characters enclosed in quotation marks (single, double, or triple) or defined with the str class. Strings are immutable, which means that once you create a string, you cannot change its contents.

Here are a few examples of string literals in Python:

Single-quote string

string1 = 'hello world'

Double-quote string

string2 = "hello world"

Triple-quote string

string3 = '''hello world'''
string4 = """hello world"""

String defined with the str class

string5 = str('hello world')

Strings in Python can contain letters, digits, and special characters. You can use escape sequences to include special characters in strings.

What is a substring?

A substring is a contiguous sequence of characters within a string. In other words, a substring is a portion of a string.

Various Ways of Finding All Occurrences of a Substring in a String

Using the find method

You can use the find method of the string class to find the index of the first occurrence of a substring in a string. Then you can use a loop to search for the next occurrences of the substring by starting the search at the index immediately following the previous occurrence.

Here is an example of how you can use the find method to find all the occurrences of a substring in a string:

def find_all(string, substring):
	indices = []
	index = 0
	while index < len(string):
    		index = string.find(substring, index)
    		if index == -1:
        			break
    		indices.append(index)
    		index += len(substring)
	return indices

# Test the function
string = 'hello world, hello moon'
substring = 'hello'
indices = find_all(string, substring)
print(indices)  # Output: [0, 14]

Using the finditer method

You can use the finditer method of the ‘re’ module to find all the occurrences of a substring in a string. The ‘finditer’ method returns an iterator that yields MatchObjects for each occurrence of the substring. Here is an example of how you can use the ‘finditer’ method to find all the occurrences of a substring in a string:

import re

def find_all(string, substring):
	indices = []
	for match in re.finditer(substring, string):
    		indices.append(match.start())
	return indices

# Test the function
string = 'hello world, hello moon'
substring = 'hello'
indices = find_all(string, substring)
print(indices)  # Output: [0, 14]

Using the count method and slicing

You can use the count method to count the number of occurrences of the substring in the string. Then use slicing to extract each occurrence along with its indices.

my_string = 'abcabcabc'
my_substring = 'abc'

# Count the number of occurrences
num_occurrences = my_string.count(my_substring)

# Initialize a list to store the indices and occurrences
occurrences = []

# Extract each occurrence and its index using slicing
for i in range(num_occurrences):
	start = i * len(my_substring)
	end = start + len(my_substring)
	occurrences.append((start, my_string[start:end]))

print(occurrences)  # prints [(0, 'abc'), (3, 'abc'), (6, 'abc')]

Conclusion

These are various methods using which we can find all the occurrences of a substring in a string. There can be various other methods as the human mind has no dearth of creativity, but these are the simpler ones and most used ones.

Finding All Occurrences of a Substring in a String (All Methods Included) in python