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))

Free Resources