Menu Close

Python String rindex() Method

Python String rindex

In this article, you will learn about the Python string rindex() method using examples. rindex function in Python is a string function that returns the last occurrence of a specified value. Python string rindex() works the same as the string rfind() method.

In the previous tutorial, we have seen the Python string replace() method to replace old string with a new string.

Python string rindex() method

Python string rindex() method is a string built-in method that is used to return the highest index of the specified value.
The rindex() method raises an exception if the value not found.

Syntax

The syntax of python string rindex method is:-

string.rindex(sub, start, end)

Parameter

rindex function in Python accepts three parameters.

  • sub:- substring to be searched.
  • start:- starting index to start searching.
  • end:- end index to stop searching.

Return Value

rindex function in Python:-

  • Return the last index if the value found.
  • Raise an exception if the value not found.

Python string rindex example.

Here we will take some example to understand rindex function in Python.

Example 1:

a = 'Python is Programming Language'
result = a.rindex('e')
print(result)

Output will be:- 29

Example 2:

a = 'Python is the most popular programming language. Python used in various software domains. Using Python we can build a web application, desktop application, etc.'
result = a.rindex('Python', 0, 50)
print(result)

Output will be:- 44

Example 3:

a = 'Python is most popular programming language.'
result = a.rindex('C++')
print(result)

Output will be:-

Traceback (most recent call last):
  File "C:\Users\user1\Desktop\tuts\jQuery\test.py", line 2, in <module>
    result = a.rindex('C++')
ValueError: substring not found

Concluion

In this tutorial, you have learned the Python string rindex method. rindex function in Python returns the highest index of the specified value.
Python string rindex() method works the same as rfind() method.

I hope this tutorial will help you. if you like this article, please share it with your friends who want to learn Python programming.

String Methods

For More Information:- Click Here

Previous Article
Next Article

Was this post helpful?

Related Posts

Leave a Reply