×

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

  1. find() method
  2. split() method
  3. 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.


Related Topics

Python Project Ideas Based On Django

Introduction If you have learned Python and you are an expert in Django, then your practical skills should be excellent in this field. If you want to check your practical skills,...

9 minutes read.

Python datetime

Python provides a module named datetime to work with the date and time. Sometimes in the real life application development, we need to work with the date and time. The date is...

3 minutes read.

How to Convert String to List In Python?

How to Convert String to List In Python? We all are familiar with what strings and lists are, let us have a quick revision on them- Strings are a sequence of characters...

4 minutes read.

Python enumerate() function

Python enumerate() function The enumerate() function in Python takes a collection (e.g. a tuple) and returns it as an enumerate object. Syntax: enumerate(iterable, start=0) Parameter Iterable:  This parameter represents an iterable object start:    This parameter represents a number defining...

1 minute read.

Python String zfill() method

Python String zfill() method The string.zfill() method in Python returns a copy of the string while adding the zeros (0) at the beginning of the string, until it reaches the specified...

1 minute read.

Python Set discard() method

Python Set discard() method The set.discard() method in Python removes a specified element from the set (if present). Syntax set.discard(value) Parameter value- This parameter represents the element to be removed from the set. Return None Example 1 # Python program explaining # the...

1 minute read.

Accessing Key-value in Dictionary in Python

A Python word reference is an assortment of key-esteem matches where each key is related to worth. Worth in the key-esteem pair can be a number, a string, a rundown,...

5 minutes read.

Python Console

Console in Python is referred as the command line Interpreter- (CLI) and also knows as Shell and it functions as taking input from the human user and interpreting it through...

6 minutes read.

Python Random shuffle( ) method

The shuffle() is used to change the positions of the elements in the mutable sequences. The shuffle( ) function will change the positions of the elements in the sequence of...

3 minutes read.

How to open a file in python

Opening a File in Python Python is a user-friendly programming language that makes almost every concept easy. It provides many inbuilt functions in its libraries to work with files. Using these...

6 minutes read.

Python Operator Precedence

Before knowing about the operator precedence in Python, we have to know about the operators in Python. So let's have a look at it. According to one definition, the operator is...

3 minutes read.

Ternary operators in python

Starting from Python version 2.5, ternary operators are also known as Conditional operators. Using these operators, we can evaluate any problem based on a condition. It is an alternative and...

4 minutes read.

Python exit commands

exit(), quit(), sys.exit(), os._exit() In this tutorial, we will study exit commands used in the Python programming language. Python is undoubtedly the choice of programmer and this is because of the in-built...

3 minutes read.

Python Encapsulation

What is meant by Encapsulation? The process of restricting access to the methods and variables so that accidental modification of data can be prevented is known as Encapsulation. The motive of Encapsulation:...

3 minutes read.

How to Convert int to str in Python

How to Convert int to str in Python In the last article, we studied how we convert a variable with string datatype to integer datatype and discussed different data types available...

4 minutes read.

Python md5_file() function

Python md5_file() function The md5_file() function in PHP calculates the md5 hash of a given file. Syntax md5_file ( string $filename [, bool $raw_output ] )  Parameter filename(required)- This parameter signifies the file to be calculated. raw_output(optional)- It takes a boolean value that specifies hex or binary...

1 minute read.

Python comment symbol

Python programmers frequently use the comment system because, without it, things may quickly become very perplexing. The developers' helpful information is provided in the comments, which helps the reader understand...

3 minutes read.

Expressions in Python

What is Expression in Python? The expression contains more than one operator as well as the operands with it. Expression helps us to produce some other values. In the Python programming...

12 minutes read.

List Index out of Range Python for Loop

The List is generally used in Python to store multiple items or elements inside one single variable. The List is ordered in nature, the List can contain the elements inside...

3 minutes read.

Python sorted() function

Python sorted() function The sorted() function returns a sorted list of the specified iterable object. Syntax sorted(iterable, *, key=None, reverse=False) Parameter iterable: It is a required parameter that represents the sequence to sort, list, dictionary, tuple etc. key:...

1 minute read.