The Python Find โ€“ String find() Method is a useful tool for finding the index of a specified substring in a given string.

It is an inbuilt function in Python and returns the index of the first occurrence of the substring if found in the specified string.

The syntax for this method is string.find(substring, start, end).

The start and end parameters are used to limit the search to a particular portion of the string.

This method is case-sensitive and returns -1 if the substring is not found in the given string.

This method is often used when we need to locate a substring within a string.

To find the second wordโ€™s position. First, we find the 1st keyword position then calculate second by using find an operator.

python string find

Let we take an example

Python find the second (nth) occurrence in the string

Python Find nth Occurrence

Finding the nth occurrence of a character in a string in Python is relatively simple. To do so, you can use the โ€œstr. find()โ€ method.

This method returns the index of the character of the first occurrence, so you can just loop through the string, keeping track of the number of occurrences of the character, until you find the nth occurrence.

Hereโ€™s an example:

my_string = "Hello World!"
# Count the number of occurrences of "l"
n = 0
for i in range(len(my_string)):
if my_string[i] == "l":
n += 1

# Find the index of the nth occurrence of "l"
nth_occurrence = 0
for i in range(len(my_string)):
if my_string[i] == "l":
nth_occurrence += 1
if nth_occurrence == n:
print("The index of the nth occurrence of 'l' is:", i)
break

Related: Python TypeCasting

How to Python find the position of the Second same substring or keyword in a given string?

Example:1

Note: In given String two โ€œwebโ€ keywords are used. Now, we can calculate the position of the second โ€œwebโ€ keyword in a given string.

First, we can calculate the first sub-string position then we can calculate the second same sub-string position.

para = "I am web Developer and SEO experts and web based application"
position1 = (para.find("web"))
#Above line is used to calculate the position of first "web".
position2 = (para.find("web"), position1+1)
#Above line is used to calculate the position of second "web" keyword.
print (position1)ย  # print first keyword position
print (position2)ย  # print second keyword position

Output:
5
39
python string find method

Description: when we find the position of the second same word in a paragraph then we used also the first position to increment (plus one).

An increment of plus one in the first position is used to calculate the position of the second same keyword.

Are you wanna know about Best web Development frameworks in 2020? then must-read.

How to check the position of the substring is correct or not?

Let we take the above example

Example:2

para = "I am web Developer and SEO experts and web based application"
position1 = (para.find("web"))
position2 = (para.find("web",position1+1))
print (position2)
print (para[position2: ])

Output: web based application

Description: The last line of the program is used to print the remaining line after the second web word to check whether it is the correct output or not.

Are you want to know more about string?, please read Python Documentation.

I hope this article is very helpful โ€œHow Python Finds nth occurrence of a substring in a stringโ€.

If you have any queries about this lecture, you will discuss it in the comments section.

Categorized in: