Use rfind() to search a string from the end. It returns the starting index of the matching substring nearest the end, or -1 when no match exists. section 8.1
Finding the last matching substring
who = "me myself and I"\nwho.rfind("el") # 6\n\nThe substring "el" occurs nearest the end beginning at index 6. By contrast, who.find("e") returns 1 because find() reports the first match. section 8.1
Quick checks with a = "python 4 ever&EVER":
- a.rfind("rev") returns -1 because the substring is absent. section 8.1
- a.rfind("VER") returns 15, while a.rfind(" ") returns 8. section 8.1
Figure 8.1. Four examples of substrings to find in the string "me myself and I". The arrows indicate the direction in which you’re finding the substring. The check mark tells you the index in which you found the substring. An x tells you that the substring wasn’t found.
Use the figure to visualize search direction, matching indexes, and unsuccessful searches. section 8.1
