Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to search a string in backwards direction in Python?
In this article, we are going to find out how to search a string in backward direction in Python.
Python provides two main methods for searching strings from right to left: rindex() and rfind(). Both methods find the rightmost occurrence of a substring but handle missing substrings differently.
Using rindex() Method
The rindex() method returns the highest index of any substring in the given string. By highest index, we mean that if a substring appears multiple times, rindex() returns the index of the rightmost occurrence.
The main disadvantage is that it throws a ValueError if the substring is not found.
Syntax
string.rindex(substring, start, end)
Example 1 ? Finding Existing Substring
In the example given below, we are finding the last index of a specific substring using the rindex() method ?
text = "Welcome to Tutorialspoint"
substring = "Tutorial"
print("The given string is:")
print(text)
print("Finding the last index of", substring)
print(text.rindex(substring))
The output of the above example is ?
The given string is: Welcome to Tutorialspoint Finding the last index of Tutorial 11
Example 2 ? Substring Not Found
When the substring is not found, rindex() raises a ValueError ?
text = "Welcome to Tutorialspoint"
substring = "Hello"
print("The given string is:")
print(text)
try:
print("Finding the last index of", substring)
print(text.rindex(substring))
except ValueError as e:
print("Error:", e)
The output of the above example is ?
The given string is: Welcome to Tutorialspoint Finding the last index of Hello Error: substring not found
Using rfind() Method
The rfind() method overcomes the drawback of rindex(). It has similar functionality but returns -1 instead of throwing an exception when the substring is not found.
Example 1 ? Finding Existing Substring
The rfind() method works identically to rindex() when the substring exists ?
text = "Welcome to Tutorialspoint"
substring = "Tutorial"
print("The given string is:")
print(text)
print("Finding the last index of", substring)
print(text.rfind(substring))
The output of the above example is ?
The given string is: Welcome to Tutorialspoint Finding the last index of Tutorial 11
Example 2 ? Substring Not Found
When the substring is not found, rfind() returns -1 without raising an exception ?
text = "Welcome to Tutorialspoint"
substring = "Hello"
print("The given string is:")
print(text)
result = text.rfind(substring)
print("Finding the last index of", substring)
print(result)
if result == -1:
print("Substring not found")
else:
print("Substring found at index:", result)
The output of the above example is ?
The given string is: Welcome to Tutorialspoint Finding the last index of Hello -1 Substring not found
Practical Example ? Multiple Occurrences
Here's an example showing how both methods handle multiple occurrences ?
text = "Python is great, Python is powerful"
substring = "Python"
print("String:", text)
print("Using rfind():", text.rfind(substring))
print("Using rindex():", text.rindex(substring))
# Show all occurrences for comparison
print("All occurrences:")
start = 0
while True:
pos = text.find(substring, start)
if pos == -1:
break
print(f"Found at index: {pos}")
start = pos + 1
The output shows that both methods return the rightmost occurrence ?
String: Python is great, Python is powerful Using rfind(): 17 Using rindex(): 17 All occurrences: Found at index: 0 Found at index: 17
Comparison
| Method | Returns When Found | Returns When Not Found | Best For |
|---|---|---|---|
rindex() |
Index position | Raises ValueError | When substring must exist |
rfind() |
Index position | Returns -1 | When substring may not exist |
Conclusion
Use rfind() for safe backward string searching that won't raise exceptions. Use rindex() only when you're certain the substring exists and want explicit error handling for missing substrings.
