Find nth Occurrence of Substring in a String in Python



While working with the strings, the common requirement is searching for a specific substring within a larger string. Python provides built-in methods that allow you to locate the first occurrence of a substring. But what if the substring appears multiple times.

For example, consider the string "Welcome to the Tutorialspoint to". If we look for the second occurrence of the substring "to", using the find() will get only the first match, but we need an approach that searches beyond the first occurrence, counting each one until the nth.

In this article, we will explore how to find the nth occurrence of a substring in a string.

Using Python find() Method

In this approach, we are going to use the Python find() method to search for a substring in the string. It returns the index of the first occurrence of the substring starting from the given index. If the substring is not found, it returns -1.

Syntax

Following is the syntax for the Python find() method -

str.find(value, start, end)

Example 1

Let's look at the following example, where we are going to find the second occurrence of "to" in the string.

def demo(a, b, c):
    x = -1
    for _ in range(c):
        x = a.find(b, x + 1)
        if x == -1:
            return -1
    return x
str1 = "Welcome to the Tutorialspoint to"
print(demo(str1, "to", 2))

The output of the above program is as follows -

17

Example 2

Consider the following example, where we are going to find the nth occurrence that does not exist in the string and observing the output.

def demo(a, b, c):
    x = -1
    for _ in range(c):
        x = a.find(b, x + 1)
        if x == -1:
            return -1
    return x
str1 = "TP Tutorialspoint"
print(demo(str1, 'p', 2))

The following is the output of the above program -

-1

Using Python split() Method

The Python split() method is used to split all the words in a string separated by a specified separator.

In this approach, we are going to define a function with arguments as a string, a substring, and the integer n. By dividing at the substring with max n+1 splits, we can locate the nth occurrence of a substring in a string.

If the size of the resultant list is higher than n+1, the substring appears more than n times. The length of the original string minus the length of the last split segment equals the length of the substring.

Syntax

Following is the syntax for the Python split() method -

str.split(separator, maxsplit)

Example

In the following example, we are going to consider the string and the substring as input and find the nth occurrence of the substring in the string.

def findnth(string, substring, n):
   parts = string.split(substring, n + 1)
   if len(parts) <= n + 1:
      return -1
   return len(string) - len(parts[-1]) - len(substring)
string = 'Hi Hello Vanakam Hi'
substring = 'Hi'
res = findnth(string,substring,1)
print(res)

The following is the output of the above program -

17
Updated on: 2025-05-20T11:00:51+05:30

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements