Python String rfind() Method
The Python rfind() method searches the given string argument in the subject string and returns the max index position of the occurrence ( last occurance ) of the string within another string.
Syntax:
This rfind() method accepts three parameters.
substr(required) | This substring to be searched in subject string. |
start(optional) | It is an integer value specifying the index position to start the search. If not specified, the default value 0 is taken. |
end(optional) | It is an integer value specifying the index position where to end the search from right side. If not specified, -1 value is taken, and mean full string length is searched |
The lowest index position of the occurrence of the string within another string. If the given string is not found, -1 is returned.
In the following example, the occurrence of 'i' is searched within the string, and the index always starts with 0. Hence it will return 4 as a result.
# Python example of the rfind() method.
str = "Nothing is impossible!!!"
result = str.rfind("i")
print(result)
Output:
If no start and end arguments are assigned.
name = 'Tutorials Nation'
result = name.rfind('Nation')
print("Substring 'Nation' occurrence found at", result)
Output:
The following example checks, If the given string is found.
name = 'Tutorials Nation'
if name.rfind('web') != -1:
print("The substring is found.")
else:
print("The substring is not found.")
Output: