I think you're expecting rfind to return the index of the rightmost character in the first/leftmost match for "what". It actually returns the index of the leftmost character in the last/rightmost match for "what". To quote the documentation:

str.rfind(sub[, start[, end]])

Return the highest index in the string where substring sub is found, such that sub is contained within s[start:end]. Optional arguments start and end are interpreted as in slice notation. Return -1 on failure.

"ab c ab".find("ab") would be 0, because the leftmost occurrence is on the left end.
"ab c ab".rfind("ab") would be 5, because the rightmost occurrence is starts at that index.

Answer from Jeremy on Stack Overflow
🌐
W3Schools
w3schools.com › python › ref_string_rfind.asp
Python String rfind() Method
The rfind() method finds the last occurrence of the specified value. The rfind() method returns -1 if the value is not found. The rfind() method is almost the same as the rindex() method.
Discussions

"Differentiate between the string methods find() and rfind()."
The correct answer is Option 3. Key Points The find() method searches for the first occurrence of a specified value in a string and returns its index More on testbook.com
🌐 testbook.com
1
1707
1 month ago
Same value returned when using find and rfind on a string in Python 3.7.0 (using idle) - Stack Overflow
Novice user of Python and Stack Overflow - first question I am perplexed of why I am getting same value for find and rfind (Python 3.7.0) for the following: >>> string="ooook" >>> More on stackoverflow.com
🌐 stackoverflow.com
October 21, 2018
if in vs. str.rfind()
First of all, those aren't strings; they're tuples. Tuples have no rfind() method, so your friend's code is broken and will not run (try it!). For the sake of illustration, let's replace that problem with one that actually does involve strings: if 'spoon' in 'my spoon is too spoony': print True This will search for the substring "spoon", and print True. If you just want to see if 'spoon' is in the larger string, this is the most straightforward and Right way to do it. Now let's look at what find() and rfind() would do: 'my spoon is too spoony'.find('spoon') # returns 3 'my spoon is too spoony'.rfind('spoon') # returns 16 These look for a substring, starting at the beginning or end of the larger string, respectively. Use one of them if you need to know the location of the substring. In general: if there are two equivalent ways of doing something, and one of them is simpler and more readable, that's the one to go with. More on reddit.com
🌐 r/Python
26
8
February 26, 2012
Python string find() examples
https://docs.python.org/3/library/stdtypes.html?highlight=str%20find#str.find https://www.w3schools.com/python/ref_string_find.asp More on reddit.com
🌐 r/learnpython
11
52
October 17, 2022
🌐
Reddit
reddit.com › r/python › if in vs. str.rfind()
r/Python on Reddit: if in vs. str.rfind()
February 26, 2012 -

a coworker asked me Python advice. I'm a n00b...This is what I offered.

substring=("foo","bar")
string=("1","2","3",("foo","bar"))

if substring in string:
    print True

He did this (something like this, pulling from memory):

string.rfind(substring) != -1
    print True

Can someone shed light on both?

Reading the documentation for rfind() it seems like it's returning the index of the substring so the != -1 would simply say 'does not exist'?

I only wonder if this is the right context to use rfind().

FWIW, this is on Python 2.4.3 if it matters.

Thanks! Much appreciated!

Top answer
1 of 5
42
First of all, those aren't strings; they're tuples. Tuples have no rfind() method, so your friend's code is broken and will not run (try it!). For the sake of illustration, let's replace that problem with one that actually does involve strings: if 'spoon' in 'my spoon is too spoony': print True This will search for the substring "spoon", and print True. If you just want to see if 'spoon' is in the larger string, this is the most straightforward and Right way to do it. Now let's look at what find() and rfind() would do: 'my spoon is too spoony'.find('spoon') # returns 3 'my spoon is too spoony'.rfind('spoon') # returns 16 These look for a substring, starting at the beginning or end of the larger string, respectively. Use one of them if you need to know the location of the substring. In general: if there are two equivalent ways of doing something, and one of them is simpler and more readable, that's the one to go with.
2 of 5
19
This benchmark is totally unnecessary, because you should always do things correctly in non-critical code even if it's a little slower, but here you go: $ python -mtimeit -s "needle = 'spam'; haystack = 'ham, spam, and eggs'" "needle in haystack" 10000000 loops, best of 3: 0.0988 usec per loop $ python -mtimeit -s "needle = 'spam'; haystack = 'ham, spam, and eggs'" "haystack.find(needle) != -1" 1000000 loops, best of 3: 0.403 usec per loop $ python -mtimeit -s "needle = 'spam'; haystack = 'ham, spam, and eggs'" "haystack.rfind(needle) != -1" 1000000 loops, best of 3: 0.434 usec per loop $ python3 -mtimeit -s "needle = 'spam'; haystack = 'ham, spam, and eggs'" "needle in haystack" 10000000 loops, best of 3: 0.0849 usec per loop $ python3 -mtimeit -s "needle = 'spam'; haystack = 'ham, spam, and eggs'" "haystack.find(needle) != -1" 1000000 loops, best of 3: 0.313 usec per loop $ python3 -mtimeit -s "needle = 'spam'; haystack = 'ham, spam, and eggs'" "haystack.rfind(needle) != -1" 1000000 loops, best of 3: 0.318 usec per loop needle in haystack wins by a factor of 4, and looks better to boot.
🌐
CodeRivers
coderivers.org › blog › find-vs-rfind-python
Understanding `find` vs `rfind` in Python - CodeRivers
February 22, 2026 - It searches from left to right (starting from the beginning of the string) and returns the lowest index where the substring is found. If the substring is not found, it returns -1. The rfind() method, on the other hand, also searches for a substring within a string.
🌐
Tutorialspoint
tutorialspoint.com › python › string_rfind.htm
Python String rfind() Method
The Python String rfind() method searches for the starting index of a last occurrence of a specified substring is found in an original string. Unlike the rindex() method, which raises an exception if the substring is not present, this method returns
Find elsewhere
🌐
Scaler
scaler.com › home › topics › rfind() in python
rfind() in Python | rfind() Function in Python - Scaler Topics
June 15, 2022 - 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. ... In the above example, we are finding the index of the last occurrence of val in txt using the rfind() method in Python.
🌐
Learn By Example
learnbyexample.org › python-string-rfind-method
Python String rfind() Method - Learn By Example
April 20, 2020 - # Search the string from position ... to the rindex() method. The only difference is that the rindex() method raises a ValueError exception, if the substring is not found....
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-string-methods-set-1-find-rfind-startwith-endwith-islower-isupper-lower-upper-swapcase-title
Python String Methods - Set 1 (find, rfind, startwith, endwith, islower, isupper, lower, upper, swapcase & title) - GeeksforGeeks
July 23, 2025 - returns -1 if the substring is ... found. 2. rfind("string", beg, end) :- This function has the similar working as find(), but it returns the position of the last occurrence of string....
🌐
Toppr
toppr.com › guides › python-guide › references › methods-and-functions › methods › string › rfind › python-string-rfind
Python rfind() function | Why do we use Python String rfind() function? |
September 27, 2021 - Python rfind() function is used ... string; else it returns -1. The Python rfind() is an in-built string method that returns the index position of the character if found; else it returns value -1....
🌐
DEV Community
dev.to › itsmycode › python-string-rfind-23mb
Python String rfind() - DEV Community
January 25, 2022 - The only major difference is that the rfind() method returns -1 if the substring is not found in a given string, whereas the rindex() method will raise the ValueError: substring not found exception. text = 'she sells seashells on the seashore' ...
🌐
Toppr
toppr.com › guides › python-guide › references › methods-and-functions › python-string-find
Python find: Python String find, Python find in List, FAQs
October 19, 2021 - # Python program to illustrate ... find() function, with the sole difference being that rfind() returns the highest index (from the right) for the provided substring, whereas find() returns the index position of the element’s ...
🌐
Sololearn
sololearn.com › en › Discuss › 2343176 › what-is-the-difference-between-rfind-index
What is the difference between rfind() & index()?
Sololearn is the world's largest community of people learning to code. With over 25 programming courses, choose from thousands of topics to learn how to code, brush up your programming knowledge, upskill your technical ability, or stay informed about the latest trends.
🌐
Scaler
scaler.com › home › topics › find in python | find() function in python
Find in Python | find() function in Python - Scaler Topic
January 19, 2024 - Here, we fist created a string variable named myStr and called find() and rfind() on it. find() returned 0 and rfind() returned 9. This is because, rfind() returned the index of the last occurence of 'py' while find() returned the index of the first occurence of 'py'. index() function is also ...
🌐
Programiz
programiz.com › python-programming › methods › string › rfind
Python String rfind()
The rfind() method returns the highest index of the substring (if found). If not found, it returns -1.
🌐
YouTube
youtube.com › jakubication
Python rfind vs find - YouTube
This video teaches about the find vs rfind string methods in Python. find returns the index the substring you are looking for starts at. rfind works similarl...
Published: December 24, 2024
🌐
Javatpoint
javatpoint.com › python-string-rfind-method
Python String | rfind() method with Examples - Javatpoint
Python String rfind() method with Examples on capitalize(), center(), count(), encode(), find(), format(), index(), join(), lower(), ljust(), isupper(), istitle(), isspace(), isprintable(), isnumeric(), islower() etc.
🌐
Medium
medium.com › @webbazaar › python-string-rfind-method-79ca69f2d446
python string rfind() method:. The rfind() method search and returns… | by convertlo | Medium
February 5, 2021 - rfind() method behaves same as rindex() do but the difference is that rfind() method returns -1 if it could not find any match in original string but rindex() method raises an exception if i could not find any match.
🌐
Programmersought
programmersought.com › article › 82781202681
The difference between find() and rfind() in Python - Programmer Sought
Python rfind() returns the first occurrence of the string (from right to left), or -1 if there is no match. Python find() returns the first occurrence of the string (from left to right), or -1 if there is no match. ... Here str is the string to be searched, beg=begin, which is the position ...