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.

The first method is to use the inbuilt python String class's rindex() method. The highest index of any substring in the given string is returned by the Python string rindex() method.

By highest index, we mean that if a given substring appears twice or three times in a string, the rindex() method will return the index of the substring's rightmost or final occurrence.

The main disadvantage of this function is that it throws an exception if the string contains no substrings.

Example 1

In the example given below, we are taking a string as input and we are finding out the last index of some specific characters using the rindex() method 

str1 = "Welcome to Tutorialspoint"
char = "Tutorial"

print("The given string is:")
print(str1)

print("Finding the last index of",char)
print(str1.rindex(char))

Output

The output of the above example is as shown below −

The given string is:
Welcome to Tutorialspoint
Finding the last index of Tutorial
11

Example 2

In the example given below, we are taking the same program as above but we are trying with different string as input 

str1 = "Welcome to Tutorialspoint"
char = "Hello"

print("The given string is:")
print(str1)

print("Finding the last index of",char)
print(str1.rindex(char))

Output

The output of the above example is given below −

The given string is:
Welcome to Tutorialspoint
Finding the last index of Hello
Traceback (most recent call last):
File "C:\Users\Tarun\OneDrive\Desktop\practice.py", line 6, in 
print(str1.rindex(char))
ValueError: substring not found

Using rfind() method

There is a method called rfind() that can be used to overcome the drawback of rindex(). Its functionality is similar to the rindex() method, but instead of throwing an exception if the given substring isn't found in the string, this method returns '-1,' indicating that the given substring isn't found.

Example 1

In the example given below, we are taking a string as input and we are finding out the last index of some specific characters using the rfind() method.

str1 = "Welcome to Tutorialspoint"
char = "Tutorial"

print("The given string is:")
print(str1)

print("Finding the last index of",char)
print(str1.rfind(char))

Output

The output of the above example is given below −

The given string is:
Welcome to Tutorialspoint
Finding the last index of Tutorial
11

Example 2

In the example given below, we are taking the same program as above but we are trying with different string as input.

str1 = "Welcome to Tutorialspoint"
char = "Hello"

print("The given string is:")
print(str1)

print("Finding the last index of",char)
print(str1.rfind(char))

Output

The output of the above example is as shown below−

The given string is:
Welcome to Tutorialspoint
Finding the last index of Hello
-1

Updated on: 07-Dec-2022

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements