Here is one way to do this:
>>> def find_second_last(text, pattern): ... return text.rfind(pattern, 0, text.rfind(pattern)) ... >>> find_second_last("abracadabra", "a") 7
This uses optional start and end parameters to search for the second occurrence after detecting the first occurrence.
Note. This does not do any sanity check and will explode if there are at least 2 occurrences of the template in the text.
MAK
source share