Remember
rfind() returns the rightmost matching substring index or -1 when the substring is absent.
rfind() returns the rightmost matching substring index or -1 when the substring is absent.
Follow the example, change its sample, and compare what happens.
Explain the result and test your understanding.
Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.
Follow the code and watch its state change. Guided simulation.
text = 'banana'
result = text.rfind("a")
print(result)Top-level scope
'banana'Object 2 · int5rfind() returns the rightmost matching substring index or -1 when the substring is absent.
(no output)
What do you expect this program to print?
EXAMPLE INSTRUCTIONS
text = 'banana'
result = text.rfind("a")
print(result)
|
v
DISPLAYED RESULT
5
FINAL BINDINGS
text --> object 1 (str)
result --> object 2 (int)
object 1: 'banana'
object 2: 5Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.
Choose a sample and follow its execution. Guided simulation.
text = 'banana'
result = text.rfind("a")
print(result)Top-level scope
'banana'Object 2 · int5The example has finished. Compare the final output with the values and references above.
5
What do you expect this program to print?
text = 'banana'
result = text.rfind("a")
print(result)rfind() MethodThe rfind() method in Python is used to find the last occurrence of a substring in a given string. It returns the highest index where the substring is found, or -1 if it's not found.
string.rfind(substring, start, end)
substring – The string to search for.start (optional) – The starting index to begin search.end (optional) – The ending index where search stops.-1 if the substring is not found.text = "hello world, welcome to the world of Python"
index = text.rfind("world")
print(index)
28
Why? The substring "world" occurs twice, and the last one starts at index 28.
text = "python programming"
print(text.rfind("java"))
-1
Why? Because "java" is not found in the string.
text = "abc abc abc"
print(text.rfind("abc", 0, 7))
4
Why? It searches only from index 0 to 7. The last "abc" in this range starts at index 4.
find() returns the first occurrencerfind() returns the last occurrence/) in a file pathrfind() with rindex() – rindex() raises an error if substring not found, rfind() returns -1.start and end which can restrict search range.Given a string containing multiple email addresses separated by commas, find the last occurrence of “@gmail.com”.
emails = "john@yahoo.com, jane@gmail.com, doe@gmail.com, someone@outlook.com"
print(emails.rfind("@gmail.com"))
38
rfind() returns the highest index of a substring-1 if the substring is not foundPractice with strings that have multiple repeating words. Try restricting the search range with start and end parameters for deeper understanding.
Connect what you noticed in the example with the idea below.
rfind() returns the rightmost matching substring index or -1 when the substring is absent.
rfind() returns an index counted backward from the end.
This is the misconception to avoid.Reference checks are optional and do not count toward course progress.