How To Find Index Of Last Occurrence Of Substring In Python String

Find index of last occurrence of substring in Python String

To find index of last occurrence of substring in Python string, we can use the rindex() and rfind() method. Follow the article to better understand.

Find index of last occurrence of substring in Python String

A relatively common problem when working with strings is that you need to find out if a substring exists. Below are some methods to find the index of the last occurrence of substring in Python string. Let’s follow them now.

Use rindex() method

The rindex() function returns the last index where the string str was found or an exception if the string str is not found.

Syntax:

string.rindex(value, start, end)

Parameters:

  • value (required item): value to look for. 
  • start (optional item): starting position, default is 0.
  • end (optional item): search end location. default is the end of a string.

Example:

  • Create 2 strings: an original string and a string containing the character to be searched.
  • Use the rindex() function to find characters in the original string.
strIntro = "Hi! my name is John. I am 25 years old. Currently my job is a developer."
strSearch = "my"

# Use the rindex() function to find character 'my' in 'strIntro'
result = strIntro.rindex(strSearch)

# print result
print("Index of last occurrence of substring is : " + str(result))

Output:

Index of last occurrence of substring is : 50

If you search for a string that does not appear, the program will return the following:

  • Create 2 strings: an original string and a string containing the character to be searched.
  • Use the rindex() function to find characters in the original string. Error message program character not found.
strIntro = "Hi! My name is John. I am 25 years old. Currently my job is a developer."
strSearch = " Frank"

# Use the rindex() function to find character 'Frank' in 'strIntro'
result = strIntro.rindex(strSearch)
print("Index of last occurrence of substring is : " + str(result))

Output:

Traceback (most recent call last):
  File "code.py", line 5, in <module>
    result = strIntro.rindex(strSearch)
ValueError: substring not found

Use rfind() method

The rfind() method returns the last index where the string str was found or returned -1 if the string str is not found.

Syntax:

string.rindex(value, start, end)

Parameters:

  • value (required item): value to look for. 
  • start (optional item): starting position, default is 0.
  • end (optional item): search end location. Default is the end of a string.

Example:

  • Create 2 strings: an original string and a string containing the character to be searched.
  • Use the rfind() function to find characters in the original string. Character not found, the return value is -1.
strIntro = "Hi! my name is John. I am 25 years old. Currently my job is a developer."
strSearch = "my"

# Use the rfind() function to find character 'my' in 'strIntro'
result = strIntro.rfind(strSearch)
print("Index of last occurrence of substring is : " + str(result))

Output:

Index of last occurrence of substring is : 50

If you search for a string that does not appear, the program will return the following:

strIntro = "Hi! My name is John. I am 25 years old. Currently my job is a developer."
strSearch = " Frank"

# Use the rfind() function to find character 'Frank' in 'strIntro'
result = strIntro.rfind(strSearch)
print("Index of last occurrence of substring is : " + str(result))

Output:

Index of last occurrence of substring is : -1

Note:

The rindex() and the rfind() methods are almost identical. The difference between the two is that when you search for a string that does not appear, rfind() will return -1, and rindex() will return an exception (like mentioned example).

Summary

If you have any questions about how to find index of last occurrence of substring in Python string, leave a comment below. I will answer your questions. Thank you for reading!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *