Python3 rfind() Method
Description
Python rfind() returns the position of the last occurrence of the string, or -1 if there is no match.
Syntax
Syntax of the rfind() method:
str.rfind(str, beg=0, end=len(string))
Parameters
- str -- The string to search for
- beg -- The starting position of the search, default is 0
- end -- The ending position of the search, default is the length of the string.
Return Value
Returns the position of the last occurrence of the string, or -1 if there is no match.
Example
The following example demonstrates how to use the rfind() function:
Example
#!/usr/bin/python3
str1 = "this is really a string example....wow!!!"
str2 = "is"
print (str1.rfind(str2))
print (str1.rfind(str2, 0, 10))
print (str1.rfind(str2, 10, 0))
print (str1.find(str2))
print (str1.find(str2, 0, 10))
print (str1.find(str2, 10, 0))
str1 = "this is really a string example....wow!!!"
str2 = "is"
print (str1.rfind(str2))
print (str1.rfind(str2, 0, 10))
print (str1.rfind(str2, 10, 0))
print (str1.find(str2))
print (str1.find(str2, 0, 10))
print (str1.find(str2, 10, 0))
The output of the above example is as follows:
5 5 -1 2 2 -1
Other Extensions
Python3 Strings