The simplest approach, IMHO, would be to just iterate over the string backwards and compare each character:
def myrfind(text, aChar):
for i in range(len(text) - 1, -1, -1):
if text[i] == aChar:
return i
return -1
Answer from Mureinik on Stack OverflowW3Schools
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....
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.
Python Reference
python-reference.readthedocs.io โบ en โบ latest โบ docs โบ str โบ rfind.html
rfind โ Python Reference (The Right Way) 0.1 documentation
Returns the index of the last occurrence of the string searched for. ... Required. The string searched for. ... Optional. Search start position. ... Optional. Search end position. ... Returns -1 if sub is not found. The rfind() method should be used only if you need to know the position of sub.
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
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.
Top answer 1 of 3
2
The simplest approach, IMHO, would be to just iterate over the string backwards and compare each character:
def myrfind(text, aChar):
for i in range(len(text) - 1, -1, -1):
if text[i] == aChar:
return i
return -1
2 of 3
1
Your function should be like (with minimal changes in current code):
def Myrfind(text,aChar):
reverseString = text[::-1]
for i, c in enumerate(reverseString): # enumerate() to iterate along with index
if c == aChar:
return len(text) - i - 1 # Return len(char) - i -1 since reverse string
else: # Return -1 if function is not
return -1 # exited by for loop
Sample run:
>>> Myrfind('Hello', 'o')
4
>>> Myrfind('Hello', 'l')
3
>>> Myrfind('Hello', 'e')
1
>>> Myrfind('Hello', 'a') # 'a' not in string
-1
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.
Beautiful Soup
tedboy.github.io โบ python_stdlib โบ generated โบ generated โบ string.rfind.html
string.rfind() โ Python Standard Library
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.
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) )
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
Code.mu
code.mu โบ en โบ python โบ manual โบ string โบ rfind
The rfind method - Index of Last Match of Substring in String in Python
The rfind method returns the index of the matching substring from the end of a string in Python. If the substring is not found, the method returns the number -1.
Educative
educative.io โบ answers โบ what-is-rfind-in-python
What is rfind() in Python?
The rfind() method in Python returns the highest index of the specified substring if that substring is found in a string.
Dive into Python
diveintopython.org โบ home โบ functions & methods โบ string methods โบ rfind()
rfind() in Python - String Methods with Examples
The rfind() method in Python is a string method that is used to find the last occurrence of a specified substring within a string. It returns the highest index of the substring within the string.