Sometimes, while working with strings, we need to find if a substring exists in the string. This problem is quite common and its solution has been discussed many times before. The variation of getting the last occurrence of the string is discussed here. Let’s discuss certain ways in which this can be performed.
Method #1 : Using rindex()
This method returns the last occurrence of the substring if present in the string. The drawback of this function is that it throws the exception if there is no substring in the string and hence breaks the code.
# Python3 code to demonstrate # Find last occurrence of substring # using rindex() # initializing string test_string = "GfG is best for CS and also best for Learning" # initializing target word tar_word = "best" # printing original string print("The original string : " + str(test_string)) # using rindex() # Find last occurrence of substring res = test_string.rindex(tar_word) # print result print("Index of last occurrence of substring is : " + str(res)) |
The original string : GfG is best for CS and also best for Learning Index of last occurrence of substring is : 28
Method #2 : Using rfind()
This is the alternate method to perform this task. The advantage that this function offers better than the above method is that, this function returns a “-1” if a substring is not found rather than throwing the error.
# Python3 code to demonstrate # Find last occurrence of substring # using rfind() # initializing string test_string = "GfG is best for CS and also best for Learning" # initializing target word tar_word = "best" # printing original string print("The original string : " + str(test_string)) # using rfind() # Find last occurrence of substring res = test_string.rfind(tar_word) # print result print("Index of last occurrence of substring is : " + str(res)) |
The original string : GfG is best for CS and also best for Learning Index of last occurrence of substring is : 28
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
Recommended Posts:
- Python | Ways to find nth occurrence of substring in a string
- Python program to find the occurrence of substring in the string
- Python | Get the string after occurrence of given substring
- Python - Maximum Consecutive Substring Occurrence
- Python | Last occurrence of some element in a list
- Python | Split on last occurrence of delimiter
- Python - Slice from Last Occurrence of K
- Python - Avoid Last occurrence of delimitter
- Python program to find occurrence to each character in given string
- Generate two output strings depending upon occurrence of character in input string in Python
- Python | Replace multiple occurrence of character by single
- Python | First occurrence of True number
- Python | Count occurrence of all elements of list in a tuple
- Python | Percentage occurrence at index
- Python | Element Occurrence in dictionary of list values
- Python | Count tuples occurrence in list of tuples
- Python | Sort tuple based on occurrence of first element
- Python | Add the occurrence of each number as sublists
- Python | Split string on Kth Occurrence of Character
- Python | Consecutive Maximum Occurrence in list
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

