Lin Ma
Lin Ma

Reputation: 10149

search sub-string from backward using Python

Wondering if there is a neat API in Python to search a string from backward (from a specific position in a string), for example, in string "Hello StackOverflow Hello Python", suppose I want to find the index of sub-string "Hello" which is previous to sub-string "Python"?

thanks in advance, Lin

Upvotes: 0

Views: 821

Answers (1)

aghast
aghast

Reputation: 15310

Yes, it's called str.rindex(). It takes arguments sub - a substring to look for, start - a starting position, and end.

It is the reverse of the index function. There are some others, like rsplit, too.

Upvotes: 3

Related Questions