Python .rfind()
Anonymous contributor
Published Oct 6, 2023
Contribute to Docs
.rfind() searches a given string for a substring starting from index 0, or alternatively, from the ith index to jth index (where i < j). The method returns the starting index of the last occurrence of the substring.
Syntax
mystr.rfind(value, start, end)
This method is called on a string mystr and returns the last index where the substring value is found by searching the original string mystr from the start index to the end index, and if the substring is not found it returns -1.
Example
In the following example, .rfind() is used to search for several substring values in the original string mystr.
mystr = "This is a python string"str1 = "Hello"str2 = "is"str3 = "py"str4 = "str"print(mystr.rfind(str1))print(mystr.rfind(str2,2,5))print(mystr.rfind(str3,0,15))print(mystr.rfind(str4,10,20))
The output of the above code will be:
-121017
All contributors
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python on Codecademy
- Learn to analyze and visualize data using Python and statistics.
- Includes 8 Courses
- With Certificate
- Intermediate.13 hours
- Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
- With Certificate
- Beginner Friendly.24 hours