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 to Code
Learn any Language as a beginner
Develop a human edge in an AI powered world and learn to code with AI from our beginner friendly catalog
šŸ† Leaderboard
Daily Coding Challenge
Solve a new coding challenge every day and climb the leaderboard

Free Resources