Python String rfind() Method
The Python rfind() method is a string method that finds the last occurrence of a specified value in a string. It has the following syntax:
string.rfind(value, start, end)
where value is the substring to be searched for, start and end are optional parameters that specify the range of indexes to search within (default is the whole string).
The rfind() method returns an integer value that represents the highest index where value is found. If value is not found, it returns -1.
Here are some examples of using the Python rfind() method:
# Example 1: Find the last occurrence of 'l' in 'Hello World'
greet = 'Hello World'
index = greet.rfind('l')
print(index)
# Output: 9
# Example 2: Find the last occurrence of 'tutorials' in a sentence
mystr = 'tutorialsteacher is a free tutorials website'
index = mystr.rfind('tutorials')
print(index)
# Output: 25
# Example 3: Find the last occurrence of 'o' between index 0 and 5
greet = 'Hello World'
index = greet.rfind('o', 0, 5)
print(index)
# Output: 4
If you have any questions about this code, you can drop a line in comment.
Comments
Post a Comment