Find all occurrences of a substring in a string in Python

In this Python tutorial, we will learn how to find all occurrences of a substring in a string.

Table Of Contents

Find all occurrences of a substring using count()

The count() method is used to count the total occurrences of the given substring in a string. It takes three parameters.

Syntax:

string.count(substring,start,end)

Parameters
1. substring is the string to be counted
2. start is an optional parameter that takes an integer such that counting is started from the given index position.
3. end is an optional parameter that takes an integer such that counting is ended up to the given index position.

It returns the number of occurrences of substring in from index positions start to end in the calling string object.

Example 1:

In this example, we will count the substring-“Python”.

# String
string1="""Python programming language. Python is object-oriented, 
Python supports database connectivity and Python supports 
List,set, tuple and dictionary"""


# Get the total number of occurrences - Python
num = string1.count("Python")

print(num)

Output:

4

We can see that “Python” occurred 4 times in the string.

Example 2:

In this example, we will count the substring-“Python” from a particular position.

# String
string1="""Python programming language. Python is object-oriented, 
Python supports database connectivity and Python supports 
List,set, tuple and dictionary"""


# Get the total number of occurrences - 
# Python from 1st position to 20th position
num = string1.count("Python",0,19)

print(num)

# Get the total number of occurrences - 
# Python from 21st position to 65th position
num = string1.count("Python",20,64)

print(num)

Output:

1
2

We can see that “Python” occurred 1 time from the 1st position to the 20th position in the string and 2 times from the 21st position to the 65th position.

Find all occurrences of a substring using startswith()

The startswith() method is used to return the starting index of a particular substring. If we want to return all occurrence indices, we have to use list comprehension.

Syntax:

[iterator for iterator in range(len(string)) if string.startswith(substring, iterator)]

Parameters
1. substring is the string to be counted
2. iterator represents the position

To return the total number of occurrences, then we can apply the len() function to it.

Syntax:

len([iterator for iterator in range(len(string)) if string.startswith(substring, iterator)])

Example 1:

In this example, we will get the starting indices of the substring-“Python” and return the total number of occurrences.

# String
string1="""Python programming language. Python is object-oriented, 
Python supports database connectivity and Python supports 
List,set, tuple and dictionary"""


# Get the all indexpositions where substring "Python" exist in the string
indices = [i for i in range(len(string1)) if string1.startswith("Python", i)]

print(indices)

# Get the total number of occurrences of "Python"
num = len([i for i in range(len(string1)) if string1.startswith("Python", i)])

print(num)

Output:

[0, 29, 57, 99]
4

We can see that “Python” occurred 4 times in the string and starting indices were also returned.

Example 2:

In this example, we will get the starting indices of the substring-“supports” and return the total number of occurrences.

# String
string1="""Python programming language. Python is object-oriented, 
Python supports database connectivity and Python supports 
List,set, tuple and dictionary"""


# Get the all indexpositions where substring "supports" exist in the string
indices = [i for i in range(len(string1)) if string1.startswith("supports", i)]

print(indices)

# Get the total number of occurrences of "supports"
num = len([i for i in range(len(string1)) if string1.startswith("supports", i)])

print(num)

Output:

[64, 106]
2

We can see that “supports” occurred 2 times in the string and starting indices were also returned.

Find all occurrences of a substring using finditer()

The finditer() method is available in the re module which is used to return the starting index of a particular substring using the start() method. If we want to return all occurrence indices, we have to use list comprehension and iterate the string using an iterator.

Syntax:

[iterator.start() for iterator in re.finditer(substring, string)]

Parameters
1. substring is the string to be counted
2. string is the actual string

It returns a sequence a list of index positions where substring exists in the string. To return the total number of occurrences, then we can apply the len() function to it.

Example 1:

In this example, we will get the starting indices of the substring-“Python” and return the total number of occurrences.

import re 

# String
string1="""Python programming language. Python is object-oriented, 
Python supports database connectivity and Python supports 
List,set, tuple and dictionary"""


# Get the all indexpositions where substring "Python" exist in the string
indices = [i.start() for i in re.finditer("Python", string1)]

print(indices)

# Get the total number of occurrences of "Python"
num = len(indices)

print(num)

Output:

[0, 29, 57, 99]
4

We can see that “Python” occurred 4 times in the string and starting indices were also returned.

Example 2:

In this example, we will get the starting indices of the substring-“supports” and return the total number of occurrences.

import re 

# String
string1="""Python programming language. Python is object-oriented, 
Python supports database connectivity and Python supports 
List,set, tuple and dictionary"""


# Get the all indexpositions where substring "supports" exist in the string
indices = [i.start() for i in re.finditer("supports", string1)]

print(indices)

# Get the total number of occurrences of "supports"
num = len(indices)

print(num)

Output:

[64, 106]
2

We can see that “supports” occurred 2 times in the string and starting indices were also returned.

Summary

We have seen how to find and return the total number of occurrences of a particular substring using count(), startswith(), and finditer() methods. The startswith() and finditer() methods used list comprehension to return all the occurrences.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top