rfind() in Python
Overview
In Python, the rfind() method returns the index of the last occurrence of a substring in the given string, and if not found, then it returns -1.
Syntax of rfind() function in Python
Syntax of the rfind() method in Python is as follows:
The string here is the given string in which the value's last occurrence has to be searched. Three arguments: value, start, and end can be passed to the rfind() method.
Parameters of rfind() in python
Parameters of the rfind() method in Python are as follows:
- value: The substring to search for in the given string.
- start (optional): The starting position from where the value needs to be checked in the given string. It is an optional parameter and 0 by default.
- end (optional): The ending position till where the value needs to be checked in the given string. It is an optional parameter and is at the end of the given string by default.
Return Values of rfind() function in Python
The rfind() method returns the index of the last occurrence of a value in the given string. If the value is not found in the given string, then the return value is -1.
Example of rfind() function in Python
Code:
Output:
Explanation:
In the above example, we are finding the index of the last occurrence of val in txt using the rfind() method in Python. The index of the last occurrence of val is found out to be 18.
What is rfind() function in Python?
The rfind() method is an inbuilt string method in Python that returns the index of the last occurrence of a substring in the given string. If the substring is not found in the given string, rfind() returns -1.
We can use the optional arguments start and end in the rfind() method to specify the start and end of the string in which we have to find the substring. The rfind() is a case sensitive method, "Scaler" and "scaler" are two different words for it.
The string rfind() method is very similar to the string rindex() method, both of them return the index of the last occurrence of a substring in the given string, the only difference between them is, when the substring is not found in the given string, the rfind() method will return -1, whereas the rindex() method will raise an error.
More Examples
Let's take a look at some more examples of the rfind() method in Python for a better understanding:
Example 1: Using rfind() with no start and end argument
Code:
Output:
Explanation:
In the above example, we are finding the index of the last occurrence of "cream" in the string "I scream you scream, we all scream ice-cream" using the rfind() method. As we can see in the given string, the substring "cream" is present at indices 3, 14, 29, and 39. The rfind() method will return the highest index among these indices, that being 39, so the return value will be 39.
Example 2: Using rfind() with start and end arguments
Code:
Output
Explanation:
In the above example, we are using the rfind() method to find the index of the last occurrence of "cream" in the string "I scream you scream, we all scream ice-cream" between the starting position - 3 and ending position - 20. As we can see in the given string between the boundaries, the substring "cream" is present at index 14, so the return value will be 14.
Example 3: Behaviour of rfind() and rindex() when the value is not found in the given string
Behaviour of rfind() method
Code:
Output
Explanation:
In the above example, we are finding the index of the last occurrence of "Pizza" in the string "Fried Chicken, Fried rice, Ramen" using the rfind() method. As we can see in the given string, the substring "Pizza" is not present at any position, so the return value will be -1.
Behaviour of rindex() method
Code:
Output:
Explanation:
In the above example, we are finding the index of the last occurrence of "Pizza" in the string "Fried Chicken, Fried rice, Ramen" using the rindex() method. As we can see in the given string, the substring "Pizza" is not present in the given string, so the rindex() method will raise a ValueError, stating that the substring is not found.
Conclusion
- The string rfind() method returns the index of the last position of a substring in the given string and returns -1 if it is not found.
- We can specify the starting and ending position of checking the substring in the given string using the start and end parameters.
- The only difference between the rfind() method and rindex() method is that if the substring is not found in the given string, rfind() method returns -1, whereas rindex() raises an error.
- The string rfind() method can also be used to check whether a substring is present in the given string or not.
See Also
Refer to these topics below: