Python String rfind() Method
The str.rfind()
method returns the highest index(last occurrence of the substring) in the sequence where the substring is found. If it does not find the substring, it returns -1
Example:
str = "six sick hicks nick six slick bricks with picks and sticks."
# Find the index position of the sub-string "six"
index = str.rfind("six")
print(index)
Output
20
Syntax
The Syntax of the str.rfind()
function is:
str.rfind(sub,start,end)
Here, str
is a string.
Parameters
The str.rfind()
function can take three parameters.
- sub (required) - The substring that needs to be searched in the string
- start (optional) - The starting index position where the search should start. The default value is 0.
- end (optional) - The ending index position where the search should stop. Default is the end of the string or length of the string.
Return Value
The str.rfind()
function returns an integer value.
- if the substring is found in the string, it returns the highest index (last occurrence) where the substring is found.
- If the substring doesn't exist in the string, it returns
-1
Difference Between rindex()
method rfind()
method
The major difference between rindex()
method rfind()
method is:
- The
rindex()
method raises aValueError
exception if the sub-string is not found in the string, while therfind()
method returns-1
. - The
rindex()
method is faster than therfind()
method because it stops searching as soon as it finds the first occurrence of the sub-string.
Example:
str = "the quick brown fox jumps over the lazy dog."
# Find the index position of the sub-string "the" using index()
index = str.rindex("the")
print(index)
# Find the index position of the sub-string "Hi" using index()
try:
index = str.rindex("Hi")
except ValueError as e:
print(e)
# Find the index position of the sub-string "the" using find()
index = str.rfind("the")
print(index)
# Find the index position of the sub-string "Hi" using find()
index = str.rfind("Hi")
print(index)
Output
31
substring not found
31
-1
Example 1: How to use rfind()
method in Python
The first call to rfind()
searches for the substring "fox" starting from index 4. Since "fox" occurs at index 16 in the string, rfind()
returns 16
.
The second call to rfind()
searches for the substring "the" in the entire string. Since "the" occurs in the string at multiple places, the rfind()
method returns the index of the last occurrence of the string, which returns 31
.
The third call to rfind()
searches for the substring "cat" in the entire string. Since "cat" does not occur in the string, rfind()
returns -1
.
The fourth call to rfind()
searches for the substring "the" from index 0 to index 10 (exclusive). Since "the" occurs at index 0 in the string, rfind()
returns 0
.
sentence = "the quick brown fox jumps over the lazy dog."
# Search for "fox" starting from index 4
x = sentence.rfind("fox", 4)
print(x) # Output: 16
# Search for "the"
x = sentence.rfind("the")
print(x) # Output: 31
# Search for "cat"
x = sentence.rfind("cat")
print(x) # Output: -1
# Search for "the" ending at index 10
x = sentence.rfind("the", 0, 10)
print(x) # Output: 0
Output
16
31
-1
0
Note: If you want to find the last occurrence of a substring in a string, you can use rfind()
. On the other hand, if you're going to find the first occurrence, you can use find()
method.
Example 2: Find the index position of a sub-string in a string using a case-insensitive search:
In this example, we perform a case-insensitive search for the sub-string "zebras" in the string "ZEBRAS zig and ZEBRAS zag". To do this, we first use the lower()
method to convert the string and the sub-string to lowercase.
Then we call the rfind()
method on the lowercase string, which searches for the first occurrence of the lowercase sub-string. Since "zebras" is found at index positions 0
and 15
, the rfind()
method returns the last occurrence, i.e., 15
.
str = "ZEBRAS zig and ZEBRAS zag"
# Find the index position of the sub-string "zebras" (case-insensitive search)
index = str.lower().rfind("zebras")
print(index)
Output
15
Reference: Python Official Docs