Python 3 – String rfind() Method
The rfind()
method in Python 3 is used to search a string for a particular substring from the end of the string. This method returns the last index where the substring is found. If the substring is not found, it returns -1
.
Syntax
str.rfind(sub[, start[, end]])
Parameters:
– sub
: The substring to be searched in the string.
– start
: The starting index from where the search begins. By default, it is 0
.
– end
: The ending index till where the search should be performed. By default, it is len(str)
.
The rfind()
method can be used with any string object. Let’s have a look at some examples.
Examples:
Example 1: Basic example
text = "Python is easy to learn and easy to code"
print(text.rfind("easy"))
Output:
29
In the above example, we have a string text
which contains the words “Python is easy to learn and easy to code”. We are using the rfind()
method to search for the substring “easy”. The output is 29
which represents the last index of the substring “easy” in the string.
Example 2: Search with start and end indexes
text = "Python is easy to learn and easy to code"
print(text.rfind("easy", 0, 20))
print(text.rfind("easy", 0, 6))
Output:
-1
-1
In the above example, we are searching for the substring “easy” in the string text
with start and end indices. In the first print statement, we are searching for the substring “easy” from the start index 0
till the end index 20
. As the substring is not found before the end index, the method returns -1
. In the second print statement, we are searching for the substring “easy” from the start index 0
till the end index 6
. The substring “easy” is not found in this range, so the method returns -1
.
Example 3: rfind() vs find()
text = "Python is easy to learn and easy to code"
print(text.rfind("easy"))
print(text.find("easy"))
Output:
29
7
In the above example, we are comparing the rfind()
method with the find()
method. Both functions are used to find a substring in a string. However, the difference is in the starting position of the search. The rfind()
method starts searching from the end of the string, whereas the find()
method searches from the beginning of the string. As a result, the rfind()
method returns 29
which is the last index of the substring “easy” in the string, whereas the find()
method returns 7
which is the first index of the substring “easy” in the string.
Example 4: rfind() with no substring found
text = "Python is easy to learn and easy to code"
print(text.rfind("hard"))
Output:
-1
In the above example, we are searching for the substring “hard” in the string text
. As the substring is not found in the string, the method returns -1
.
Conclusion
In this article, we learned about the rfind()
method in Python 3. We saw how to use the rfind()
method to search for a substring in a string from the end of the string. We also saw how to use the method with start and end indices. Finally, we compared the rfind()
method with the find()
method and saw how they differ in their starting position for the search.