Python string Method - rfind()

The rfind() method is a built-in string method in Python that returns the index of the last occurrence of a specified substring in a string. If the substring is not found, the method returns -1.

The syntax for using rfind() method is as follows:

r‮fe‬er to:theitroad.com
string.rfind(substring, start, end)

Here, string is the original string, substring is the substring to search for in the string, start is the starting index of the search (optional), and end is the ending index of the search (optional). If start and end are not specified, the search will begin at the end of the string.

The method returns the index of the last occurrence of substring in the string, or -1 if the substring is not found.

Here's an example of using the rfind() method:

string = "Hello, World!"
last_index = string.rfind("l")
print(last_index)

Output:

10

In the example above, the rfind() method was used to find the index of the last occurrence of the letter "l" in the original string. The resulting index is stored in the variable last_index. Note that the search started from the end of the string and found the "l" in "World" at index 10.