How to find index of last occurrence of a substring in a string in Python?

PythonServer Side ProgrammingProgramming

Python has a rfind() method which searches from the end of a string for the occurrence of a substring. It returns the index of last occurrence if found, else -1. You can use it as follows:

>>> 'some of the some'.rfind('some')
12
>>> 'some of the some'.rfind('none')
-1
>>> "NikolaTesla".rfind('kola')
2
raja
Published on 11-Dec-2017 11:32:21
Advertisements