How to find index of last occurrence of a substring in a string in Python?



A string is a collection of characters that can represent a single word or a whole sentence. We can declare a string or any other datatype in python without the datatype specifier. Therefore, it is easy to use strings in Python compared to Java.

A string is an object of the String class, which contains multiple methods to manipulate and access strings.

In this article, we are going to focus on how to find index of last occurrence of a substring in a string in Python.

Using the rindex() method

One way to find the index of last occurrence of a substring is using the rindex() method from the inbuilt python String class. It accepts a string representing the substring to be found as a parameter, and returns the last index of the given string in the current one.

The main drawback of this function is that it throws an exception if there is no substring present in the string.

To overcome the drawback of rindex(), there is another function named rfind(). Its functionality is similar to that of rindex(), but this method does not return an exception if the given substring is not present in the string, instead, it returns ‘ 1’ indicating that the given substring is not present.

Both rindex() and rfind() have optional parameters where you can mention the start index from where you would want to start and the end index

Example 1

In the example given below, we are using the method rindex() to find out the last occurrence of the character ‘t’ in the given string.

str1 = "Welcome to tutorialspoint" index = str1.rindex('t') print("The last index of t in the given string is",index)

Output

The output of the above given example is,

('The last index of t in the given string is', 24)

Example 2

In the example given below, we are trying to find out the last index of the character ‘d’ in the given string using rindex() method, but the character ‘d’ is not present in the string.

str1 = "Welcome to tutorialspoint" index = str1.rindex('d') print("The last index of t in the given string is",index)

Output

The output of the above given example is,

Traceback (most recent call last):
  File "main.py", line 3, in 
    index = str1.rindex('d')
ValueError: substring not found

Example 3

In the example given below, we are using the method rfind() to find out the last occurrence of the character ‘t’ in the given string.

str1 = "Welcome to tutorialspoint" index = str1.rfind('t') print("The last index of t in the given string is",index)

Output

The output of the above given example is,

('The last index of t in the given string is', 24)

Example 4

In the example given below, we are trying to find out the last index of the character ‘d’ in the given string using the rfind() method, but the character ‘d’ is not present in the string.

str1 = "Welcome to tutorialspoint" index = str1.rfind('d') print("The last index of t in the given string is",index)

Output

The output of the above example is,

('The last index of t in the given string is', -1)

Advertisements