Methods and Functions

Python String rindex()

There might arise a situation where the user might want to find out the position of a specific data element in the iterable. This operation can be performed by using the string handling methods provided by Python. For this issue, the Python rindex() method is used. Python rindex() is a string method that helps to identify the index position of the specified characters from the input string. Let us learn more about the Python rindex() string function, its syntax along with various program examples.

Python rindex() function

Definition

  • Python rindex() built-in function is used to return the highest index value of the last occurrence of the substring from the input string, if the substring is found. If the substring specified is not found, then it raises an exception.
  • The Python rindex() string method returns the last index where the substring was found from the input string. If no string is found then the function raises an exception.

Python rindex() Syntax

The syntax for the Python rindex() string function is as follows:

                    

string.rindex(substr, start, end)

Note – This method is case sensitive, for example, it will consider python and Python as 2 different words.

rindex() Parameters

The rindex() function accepts 3 parameters:

  • substr (Required) – the substring to be searched in the string
  • start (Optional) – the start position from where the substring is to be searched
  • end (Optional) – the end position till where the substring is to be searched

Note – If the start and end parameters are not specified, the default values of start = 0 and end = length-1 (end of the string) are considered.

The start index and end index parameters are useful if you want to limit your search for the substring to a specific part of the string. For example, if you have a string with 100 characters and want to search for the substring from the 30th to the 80th character, you can use the start index and end index parameters which make your job easier.

Return value from rindex()

The Python rindex() function returns the following:

  • If the substring is found inside the input string, it returns the highest index value position at which the last occurrence of the substring was found. It returns an integer value.
  • If the substring was not found in the input string, the function throws a ValueError exception.

Example 1: Python rindex() with No start and end Arguments

If the substring is found multiple times in the input string, the rindex() only returns the index value of the last occurrence.

Note – The indexing in Python starts from 0 and not 1. To understand the basic operability of Python rindex(), let us look at the subsequent program.

Example

                    

# Python program to illustrate rindex()
msg = 'I am going to eat food at night'
print("Index of substring 'going':", msg.rindex('going'))
print("Index of substring 'eat':", msg.rindex('eat'))
print("Index of substring 'at':", msg.rindex('at'))
print('')

msg2 = 'Python Programming python programming'
# method is case sensitive
print("Index of substring 'python':", msg2.rindex('python'))
# notice difference between Pro and pro
print("Index of substring 'Pro':", msg2.rindex('Pro'))
# searches index of last occurrence of m
print("Index of substring 'm':", msg2.rindex('m'))
# searches index of last occurrence of i
print("Index of substring 'i':", msg2.rindex('i'))

Output

                    

Index of substring 'going': 5
Index of substring 'eat': 14
Index of substring 'at': 23


Index of substring 'python': 19
Index of substring 'Pro': 7
Index of substring 'm': 33
Index of substring 'i': 34

Example 2: rindex() With start and end Arguments

Example

                    

# Python program to illustrate rindex()
s = 'are you having a party at your house?'

# specifying start and end parameters
print("Index of 'a':", s.rindex('a', 5, 40))
print("Index of 'a':", s.rindex('a', 5, 15))

print("Index of 'ar':", s.rindex('ar', 0, 12))
print("Index of 'you':", s.rindex('you', 0, 40))

# substring not found
print("Index of 'dinner':", s.rindex('dinner', 0, 40))

Output

                    

Index of 'a': 23
Index of 'a': 9
Index of 'ar': 0
Index of 'you': 26

Traceback (most recent call last):
File "", line 11, in 
ValueError: substring not found

Difference between rindex() and rfind()

Both rindex() and rfind() are identical in that they return the highest index (or rightmost occurrence in the event of multiple occurrences) of the substring from the main string.

The main difference is that Python rfind() produces -1 as output if it is unable to find the substring, whereas rindex() throws a ValueError exception.

Example

                    

s = 'Car, Bike, Bus, Cycle'
x = s.rfind('Plane')
print(x)

x = s.rindex('Plane')
print(x)

Output

                    

-1
Traceback (most recent call last):
File "", line 5, in 
ValueError: substring not found

Frequently Asked Questions

Q1. What is rindex in Python?

There might arise a situation where the user might want to find out the position of a specific data element in the iterable. This operation can be performed by using the string handling methods called as Python rindex() method.

Python rindex() built-in function is used to return the highest index value of the last occurrence of the substring from the input string if the substring is found. If the substring specified is not found, then it raises an exception.

Example

                    

s = 'Car, Bike, Bus, Cycle, Plane'

# without start & end parameters
x = s.rindex('Bus')
print("Index value of 'Bus':" ,x)

# with start & end parameters
x = s.rindex('C', 0, 10)
print("Index value of 'C':" ,x)

Output

                    

Index value of 'Bus': 11
Index value of 'C': 0

Q2. What is rjust in Python?

The string rjust() method returns a new string of the specified length after inserting a specified character in the original string’s left side i.e. it returns a right-justified string of the specified width. The syntax followed by the Python rjust() function is as follows:

                    

string.rjust(width, fillchar)

Example

                    

s = 'Python'
width = 10
print('Original string:', s)
print('Modified string:', s.rjust(width))

s = 'Programming'
width = 18
# specifying fillchar parameter
fillchar = '^'
print('Original string:', s)
print('Modified string:', s.rjust(width, fillchar))

Output

                    

Original string: Python
Modified string:     Python

Original string: Programming
Modified string: ^^^^^^^Programming

Q3. What is rpartition in Python?

Python rpartition() function is used to partition a string at the last occurrence of the given string and return a tuple that includes 3 parts – the part before the separator, the argument string (separator itself), and the part after the separator.

The syntax for Python string rpartition() is as follows:

                    

string.rpartition(separator)

Example

                    

# Python program to illustrate rpartition()
string = 'Hello my name is Jimmy'
# when separator is found
print(string.rpartition('is'))

# when separator is not found
print(string.rpartition('who'))

string2 = 'Cricket is fun to watch, isnt it true?'
# when separator occurs multiple times in the string
print(string2.rpartition('is'))

Output

                    

('Hello my name ', 'is', ' Jimmy')
('', '', 'Hello my name is Jimmy')
('Cricket is fun to watch, ', 'is', 'nt it true?')

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

Your email address will not be published. Required fields are marked *

Download the App

Watch lectures, practise questions and take tests on the go.

Customize your course in 30 seconds

No thanks.