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
Python Examples Python Compiler ... Plan Python Interview Q&A Python Training ... The rfind() method finds the last occurrence of the specified value....
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-string-rfind-method
Python String rfind() Method - GeeksforGeeks
February 21, 2025 - Python String rfind() method returns the rightmost index of the substring if found in the given string.
Discussions

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
Why do rfind and find return the same values in Python 2.6.5? - Stack Overflow
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". More on stackoverflow.com
๐ŸŒ stackoverflow.com
string - implement rfind in Python - Stack Overflow
I am attempting to implement the rfind function in Python without using the built-in rfind method. It should work like the following: Unlike the original method, takes an input string and a charact... More on stackoverflow.com
๐ŸŒ stackoverflow.com
How to find the last occurrence of an item in a Python list - Stack Overflow
Say I have this list: li = ["a", "b", "a", "c", "x", "d", "a", "6"] As far as help showed me, there is not a builtin function that returns the last occurrence of a string (like the reverse of inde... More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
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.
๐ŸŒ
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.
๐ŸŒ
Codecademy
codecademy.com โ€บ docs โ€บ python โ€บ strings โ€บ .rfind()
Python | Strings | .rfind() | Codecademy
October 6, 2023 - .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.
Find elsewhere
๐ŸŒ
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
๐ŸŒ
Python Reference
python-reference.readthedocs.io โ€บ en โ€บ latest โ€บ docs โ€บ str โ€บ rfind.html
rfind โ€” Python Reference (The Right Way) 0.1 documentation
The rfind() method should be used only if you need to know the position of sub. To check if sub is a substring or not, use the in operator: >>> 'Py' in 'Python' True ยท >>> "ABAB".rfind("B") 3 >>> "ABAB".rfind("B", 0, 2) 1 >>> "ABAB".rfind("B", 2) 3 ยท
๐ŸŒ
Beautiful Soup
tedboy.github.io โ€บ python_stdlib โ€บ generated โ€บ generated โ€บ string.rfind.html
string.rfind() โ€” Python Standard Library
string.rfind() View page source ยท string.rfind(s, sub[, start[, end]]) โ†’ int[source]ยถ ยท Return the highest index in s 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.
๐ŸŒ
GUVI
guvi.in โ€บ hub โ€บ python-built-in-functions-tutorial โ€บ python-string-rfind
Understanding Python String rfind()
The Python rfind() string method is used to return the highest index(right most occurence) of the substring in the given string.
๐ŸŒ
Python documentation
docs.python.org โ€บ 3 โ€บ library โ€บ re.html
re โ€” Regular expression operations
Source code: Lib/re/ This module provides regular expression matching operations similar to those found in Perl. Both patterns and strings to be searched can be Unicode strings ( str) as well as 8-...
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ rfind() in python
rfind() in Python | rfind() Function in Python - Scaler Topics
June 15, 2022 - 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.
๐ŸŒ
Python.org
discuss.python.org โ€บ python help
`str.rfind()` slower than `str.find()` on macOS - Python Help - Discussions on Python.org
May 24, 2024 - That doesnโ€™t even make sense. from timeit import timeit print(timeit("string.find('/')", setup="string = 'a' * 1000")) print(timeit("string.rfind('/')", setup="string = 'a' * 1000")) wannes@Stefans-iMac cpython % main/python.exe find_str.py 0.04677163400265272 0.6524513209988072 Specs: Processor ...
๐ŸŒ
TutorialKart
tutorialkart.com โ€บ python โ€บ python-string-rfind
Python String rfind() - Examples
April 3, 2023 - Python String.rfind() method returns the index of last occurrence of specified value in given string. The search for given value in this string happens from
๐ŸŒ
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 - str = "geeksforgeeks is for geeks" s = "geeks" # using find() to find first occurrence of s print ("The first occurrence of s is at : ", end="") print (str.find( s, 4) ) # using rfind() to find last occurrence of s print ("The last occurrence of s is at : ", end="") print ( str.rfind( s, 4) )
๐ŸŒ
Skytowner
skytowner.com โ€บ explore โ€บ python_string_rfind_method
Python String | rfind method with Examples
Python's str.rfind(~) method returns the index of the last occurrence of the specified substring in the source string. If the substring is not found, then -1 is returned.