Python String rindex() Method Tutorial

In this section, we will learn what String rindex() method is and how to use it in Python.

What is String rindex() Method in Python?

The Python string rindex() method is used to find the index number of the last occurrence of a value in a string.

Python String rindex() Method Syntax:

string.rindex(value, start, end)

String rindex() Method Parameter

The method takes 3 arguments:

  • The first argument is the value that we want to look for in the target string.
  • The second argument is the index number where we want this search to start in the string. This value is optional and the default is set to 0.
  • The last argument is the index number where we want this search to end there. This value is optional and the default is set to the size of the string.

String rindex() Method Return Value

The return value of this method is the index number of the target value’s last occurrence (the first argument of the method).

Note: if the target string didn’t have the specified value (the first argument) then you’ll get an exception.

Example: using python string rindex() method

s1 = "*************My name is John and my last name is Doe**************"

print(s1.rindex("is"))

print(s1.index("is"))

Output:

46

21
Facebook
Twitter
Pinterest
LinkedIn

Top Technologies