Python Ways to find nth occurrence of substring in a string
Introduction
In Python, a string is a collection of characters that can be employed to conduct additional operations. In Python, a substring is a group of characters that are a part of some other string.
We will examine various methods to discover the n - th position of the substring with Python and retrieve the index in which the substring appears just after nth occurrence.
We perform the operations to find the nth occurrences of a substring by using the below said methods. They are
- find() method
- split() method
- Using regular expression
1. find()
To locate the position of the very first appearance of the given value in Python, use the find() function. The find() function also allows us to define a beginning and a finishing index. We are instructed to confine our search to the given range by using these starting and finishing indexes. We used the find() method on the string, which will in each iteration locate the first instance of the substring throughout our input sequence.
Code
# example python program for finding the nth
# occurrences of a substring in the string Using
# find() method
# declare string
string = "itcseitcseitcseece"
# give the substring
str = "cse"
# iterated times is
n = 2
# declare output var as -1
output = -1
# implement for loop
for i in range(0, n):
output = string.find(str,output+1)
# print the result
print ("The Nth occurrence of the substring is ", c)
Output
The Nth occurrence of the substring is 7
Due to the output value being originally set to -1 in our code's find() function, the entire string will indeed be examined from the 0th index to a conclusion in the first iteration. We will get the first instance of the split in our initial string from this iteration. The string will be searched from index 1 to the end mostly on second iteration, though. We will get the second appearance of our substring from this iteration. To find the nth instance of the string, we can perform such n repetitions.
2. split()
The provided text can be divided into such a collection of strings using a defined separator using the split() function in Python. Where the string needs to be broken, however, the separator (flat screen) can be specifically specified. Maxsplit, a secondary parameter that would be an argument towards the split() function, is used to determine how many repetitions the string must be split when a substring is discovered.
Code
# example python program for finding the nth
# occurrences of a substring in the string Using
# split() method
def solve(string, s, n):
div = string.split(s, n)
if len(div) <= n:
return -1
return len(string) - len(div[-1]) - len(s)
# print the result
print(solve('friend theend endeavour bend', 'end', 4))
Output
25
In the programme above, there is a function called solution() whose core logic is carried out. When substring s is supplied as a separator and indeed the amount of n is provided as the result of maxsplit in the initial line's application of the split() method. The indexes where we locate our substring s are where the original string is divided. Nevertheless, due to the value of n, this split only occurs twice. That at a certain index, the last string kept in the div variable equals our substring, although we haven't split them.
Following the split() method, we tested a condition to see if the length of the div variable exceeded the value of n. If this was the case, we would need to return -1 because the user might be trying to find the nth instance of a substring that doesn't exist n times. As we have only divided the string n times, our basic logic now determines the index of the substring's nth occurrence. As a result, the final element of the div variable is stored as the string that may have remained after the nth instance of the substring.
As a result, we divide the length from the last string inside the div variable, which can be accessed as div[-1], by the length of the previous string string. Since we also need to have the beginning index, we will deduct the substring length from this value, which gives the index where its recurrence of our targeted substring ends. This will enable us to determine the index of the nth instance of a substring.
3. Regular Expression
A series of characters can be used to create a search pattern when using regular expressions to discover a certain pattern in a string. The re module for regular expressions is available in Python. For locating the nth instance of the substring, we'll use the re package.
Code
# example python program for finding the nth
# occurrences of a substring in the string Using
# regular expression
# import regular expression
import re
# instantiate any string
string = "hiiamhiiam"
# give number of iterations
n = 2
output = [x.start() for x in re.finditer(r"hi" , string)]
if(len(output)<=n):
# print the result
print(output[n-1])
Output
5
To be using regular expressions inside the program above, we installed the re library in the very first line. The input has then been defined after that. All or most of the substring's beginning indexes are located using the for loop, and they are then saved in the outcome variable. Therefore, because we verify the relationship between the time of the results page and the n variable, if the user inputs a number for n that wasn't in the string, a warning will be raised.
We employ the finditer() procedure from the re package, which provides us with the starting and stopping indexes of every substring that matches the main string, but we only require the beginning index to locate the nth instance. As a result, we employ the x.start() operation, which only returns the commencing indexes of the selected substring. The nth instance of the substring is printed after our index.