Computer >> Computer tutorials >  >> Programming >> Python

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


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