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-1on 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.
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-1on 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.
find() will return the index of the first match. But rfind will give you the last occurence of the pattern. It will be clear if you try to match repeated match case.
check this Example >>> string='hey! how are you harish'
>>>string.find('h')
>>>0 #it matched for first 'h' in the string
>>> string.rfind('h')
22 #it matched for the last 'h' in the string
"Differentiate between the string methods find() and rfind()."
Same value returned when using find and rfind on a string in Python 3.7.0 (using idle) - Stack Overflow
if in vs. str.rfind()
Python string find() examples
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 TrueHe did this (something like this, pulling from memory):
string.rfind(substring) != -1
print TrueCan 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!
If you are using ipython (I can warmly recommend) you can type ?? before a command in order to see its docstring.
Doing so for string.rfind:
Docstring:
S.rfind(sub[, start[, end]]) -> int
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.
Return -1 on failure.
Type: builtin_function_or_method
and for string.find:
Docstring:
S.find(sub[, start[, end]]) -> int
Return the lowest 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.
Return -1 on failure.
Type: builtin_function_or_method
I took the liberty to highlight the important parts.
What it means is that both will return the same index if there is only one substring (i.e. 'k' in your case) found.
If you are still unsure about how str.rfind and str.find differ from each other, try the same thing with:
string = 'kooook'
Hope that helps and happy coding!
As far as I know, rfind() do the same as find(), but return the last index.
Being only one 'k', the two of the return the same. With 'o', the result will be different.