What is the rindex() method for strings in Python?

When we develope in Python, we often have to search within strings. The strings’ rindex() method is used to search the last occurrence within a string. When it fails, it raises an exception.

Syntax

string.rindex(substring[, start[, end]] )
  • substring is the string we are searching.
  • start and end are not mandatory and can be used to define a substring where the search should be done. Start is 0 by default.

Example

mystring = "Hello educative!"
print(mystring.rindex('e'))
print(mystring.rindex('e', 4, 9))
New on Educative
Learn any Language for FREE all September 🎉
For the entire month of September, get unlimited access to our entire catalog of beginner coding resources.
🎁 G i v e a w a y
30 Days of Code
Complete Educative’s daily coding challenge every day in September, and win exciting Prizes.

Free Resources