
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
How to find the nth occurrence of substring in a string in Python?
In this article, we are going to find out how to find the nth occurrence of a substring in a string in Python.
The first approach is by using the split() method. We have to define a function with arguments as string, substring, and the integer n. By dividing at the substring with max n+1 splits, you may 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.
Example
In the example given below, we are taking a string and a substring as input and we are finding the nth occurrence of the substring in the string using split() method −
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 = 'foobarfobar akfjfoobar afskjdf foobar' print("The given string is") print(string) substring = 'foobar' print("The given substring is") print(substring) res = findnth(string,substring,2) print("The position of the 2nd occurence of the substring is") print(res)
Output
The output of the above example is as follows −
The given string is foobarfobar akfjfoobar afskjdf foobar The given substring is 34. How to find the nth occurrence of substring in a string in Python foobar The position of the 2nd occurence of the substring is 31
Using find() method
The second approach is by using the find() method. This method is performed the number of occurrences times and the final result is returned.
Example
In the example given below, we are taking a string and substring as input and we are finding the nth occurrence of the substring in the string −
string = 'foobarfobar akfjfoobar afskjdf foobar' print("The given string is") print(string) substring = 'foobar' print("The given substring is") print(substring) n = 2 res = -1 for i in range(0, n): res = string.find(substring, res + 1) print("The position of the 2nd occurence of the substring is") print(res)
Output
The output of the above example is given below −
The given string is foobarfobar akfjfoobar afskjdf foobar The given substring is foobar The position of the 2nd occurence of the substring is 16
- Related Articles
- How to find index of last occurrence of a substring in a string in Python?
- How is it possible in MySQL to find the location of the first occurrence of a substring in a string?
- String function to replace nth occurrence of a character in a string JavaScript
- Get the first occurrence of a substring within a string in Arduino
- Get the last occurrence of a substring within a string in Arduino
- Create a polyfill to replace nth occurrence of a string JavaScript
- How to check if string or a substring of string starts with substring in Python?
- How to replace the last occurrence of an expression in a string in Python?
- Program to find length of longest repeating substring in a string in Python
- How to count the occurrence of a specific string in a string in JavaScript
- Python program to find occurrence to each character in given string
- How to find a substring from a string in C#?
- Program to Find Out the Smallest Substring Containing a Specific String in Python
- How to extract a substring from inside a string in Python?
- Python Program to Remove the nth Occurrence of the Given Word in a List where Words can Repeat
