Use .rfind():

>>> s = 'hello'
>>> s.rfind('l')
3

Also don't use str as variable name or you'll shadow the built-in str().

Answer from Rik Poggi on Stack Overflow
🌐
W3Schools
w3schools.com › python › ref_string_rfind.asp
Python String rfind() Method
Remove List Duplicates Reverse ... Python Interview Q&A Python Bootcamp Python Training ... The rfind() method finds the last occurrence of the specified value....
🌐
Tutorialspoint
tutorialspoint.com › python › string_rfind.htm
Python String rfind() Method
In the given program, we pass three parameters to the method called on an input string created: a substring, a beginning index and an ending index. The method searches for the specified substring within the limit and returns the index.
🌐
Vultr Docs
docs.vultr.com › python › standard-library › str › rfind
Python str rfind() - Find Substring Reverse | Vultr Docs
December 23, 2024 - In this example, rfind() finds the substring "Python" starting its search from the end of the string.
🌐
Mimo
mimo.org › glossary › python › string-find
Python string.find(): Syntax, Usage, and Examples
Use .rfind() to Search from the Right: If you need to find the last occurrence of a substring, use the .rfind() method, which searches from the end of the string backward. ... Become a Python developer.
🌐
Tutorialspoint
tutorialspoint.com › python › string_find.htm
Python String find() Method
If these two parameters are not specified, then the find() function works from the 0th index to the end of the string. If the substring is not found in the input string, '-1' is returned as output.
🌐
Quora
quora.com › How-can-we-find-the-end-of-a-string-in-Python
How can we find the end of a string in Python? - Quora
Answer (1 of 4): Using Regex libraries. You import Regex (import re) and all its search or match function.
Find elsewhere
🌐
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 - The default value is the end of string or length of the string. The Python find() method returns an integer value: It returns an index position of the first occurrence of the substring, only if the substring exists in the input string.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-string-rfind-method
Python String rfind() Method - GeeksforGeeks
February 21, 2025 - Example 5: rfind('geeks', -5) returns 7, finding 'geeks' from index -5. Here, we check one email address and the Top Level Domain (TLD) matching our necessary condition. Then we print, "Email matched" else "Email not matched", followed by the TLD String.
🌐
Code.mu
code.mu › en › python › manual › string › endswith
The endswith method - checking from the end of a string in Python
Let's find the substring 'a' by changing the search indices: txt = 'abcadea' print(txt.endswith('a', 0, 3)) Result of code execution: False · Now let's check if our substring ends with 'a': txt = 'abcadea' print(txt.endswith('a')) Result of code execution: True · method startswith, which checks a substring from the beginning of a string ·
🌐
Guru99
guru99.com › home › python › python string find() method with examples
Python String find() Method with Examples
August 12, 2024 - The string find() function will ... value from where the search for substring will begin. By default, it is 0. end: (optional) The end value where the search for substring will end....
🌐
AskPython
askpython.com › home › python string find()
Python String find() - AskPython
January 28, 2020 - We can only specify the starting position, until the end of the string, by leaving out the end argument. test_string = "Hello from AskPython" # Will print 11 print(test_string.find('AskPython', 0)) # Will print -1, as the search starts # from ...
🌐
Python Tutorial
pythontutorial.net › home › python string methods › python string find()
Python String find(): How to Find a Substring in a String Effectively
November 16, 2023 - Summary: in this tutorial, you’ll learn how to use the Python string find() method effectively to find a substring in a string. The find() is a string method that finds a substring in a string and returns the index of the substring. The following illustrates the syntax of the find() method: str.find(sub[, start[, end]])Code language: CSS (css)
🌐
GeeksforGeeks
geeksforgeeks.org › python-string-find
Python String find() Method - GeeksforGeeks
April 26, 2025 - Example 1: We can limit the search to a specific portion of the string by providing start and end parameters. ... The search starts from index 4, skipping the first "abc". The next occurrence of "abc" is found at index 8. Example 2: The find() method is case-sensitive, so uppercase and lowercase letters are treated differently. ... Explanation: Since "python" (lowercase) does not match "Python" (uppercase), the method returns -1.
🌐
Programiz
programiz.com › python-programming › methods › string › find
Python String find()
print(quote.find('small things', 10)) # Substring is searched in ' small things with great love' print(quote.find('small things', 2)) # Substring is searched in 'hings with great lov'
🌐
W3Schools
w3schools.com › python › ref_string_find.asp
Python String find() Method
Remove List Duplicates Reverse a String Add Two Numbers · Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Practice Problems Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Bootcamp Python Training ... The find() method finds the first occurrence of the specified value.
🌐
Stack Overflow
stackoverflow.com › questions › 42329212 › python-find-to-return-the-end-of-found-string
file - Python .find() to return the end of found string - Stack Overflow
Also consider that the notation string[start:end] will give you the substring including the character at start but excluding the character at end. Therefore you could use something like the following: def setAI(difficulty): configFile = open('AISettings.txt') config = configFile.read() start = config.find(difficulty) + len(difficulty) + 1 end = config.find(']', start) + 1 print(config[start:end])
🌐
Programiz
programiz.com › python-programming › methods › string › endswith
Python String endswith()
If the string ends with any item of the tuple, endswith() returns True. If not, it returns False ... result = text.endswith(('python', 'easy', 'java')) #prints True print(result) # With start and end parameter # 'programming is' string is checked
🌐
GeeksforGeeks
geeksforgeeks.org › python-find-last-occurrence-of-substring
Python | Find last occurrence of substring - GeeksforGeeks
April 17, 2023 - The re (regular expression) module in Python allows you to search for patterns in strings. In this approach, we use the finditer function from the re module to find all occurrences of the substring in the string.