Python String rindex() Method



Python string method rindex() searches for the last index where the given substring str is found in an original string. This method raises an exception if no such index exists, optionally restricting the search to string length, i.e. from the first index to the last. This method is almost similar to the rfind() method, but the difference is that the rfind() method does not raise an exception (Value Error) and returns -1 if the substring is not found.

However, if there are multiple occurrences of the same substring, the methods finds the last index of the last occurence.

Syntax

Following is the syntax for Python String rindex() method −

str.rindex(str, beg=0 end=len(string))

Parameters

  • str − This specifies the string to be searched.

  • beg − This is an optional parameter, and represents the starting index. Its default is 0

  • len − This is an optional parameter, and represents the ending index, Its default is equal to the length of the string.

Return Value

This method returns last index if found otherwise raises an exception if str is not found.

Example

When two strings are taken as input and, both rindex() and index() methods are called; the index of the first occurrence of substring is returned by the index() method while the index of the last occurrence is returned by the rindex() method.

The following example shows the usage of Python String rindex() method.

 
str1 = "this is string example....wow!!!";
str2 = "is";

print str1.rindex(str2)
print str1.index(str2)

When we run above program, it produces following result −

5
2

Example

When we input a string and pass another string as a substring parameter, the method returns the last index of the substring.

In this example, we create a string and invoke the rindex() method by passing a substring as an argument.

 
# inputting a string
string = 'Tutorialspoint is a great place to learn Python. Tutorialspoint is an ian company'
# finding the 'Tutorialspoint' using rindex
print(string.rindex('Tutorialspoint'))
# finding the 'is' using rfind
print(string.rindex('is'))

The output produced by the program above is given as follows −

49
64

Example

If we pass three parameters (including optional) to the method, the last index of the substring within the given index limit is detected.

In this example, the method is called on an input string and it takes three parameters: a substring to be located, start index and the end index.

 
# inputting a string
string = 'Tutorialspoint is a great place to learn Python. Tutorialspoint is an ian company'
# finding the 'Tutorialspoint' using rindex
print(string.rindex('Tutorialspoint', 0, 45))
# finding the 'is' using rfind
print(string.rindex('is', 0, 45))

Let us run the program above, and obtain the output as follows −

0
15

Example

If the substring given as the parameter to the method does not exist in the string, the method raises a ValueError.

In the given program, a string is created and the method is invoked on it. The substring passed as a argument to the method is not a part of the input string, the method raises an exception.

 
# inputting a string
string = 'Tutorialspoint is a great place to learn Python. Tutorialspoint is an ian company'
# finding the 'Tutorialspoint' using rindex
print(string.rindex('tutorialspoint'))

When we try to run the program, the ValueError is raised instead of printing the output −

---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
 in 
3
4 # finding the 'Tutorialspoint' using rfind
----> 5 print(string.rindex('tutorialspoint'))
ValueError: substring not found

Example

Even if the substring is present in the string but not within the limit provided by the optional parameters passed to this method, a ValueError is raised.

 
# inputting a string
string = 'Tutorialspoint is a great place to learn Python. Tutorialspoint is an ian company'
# finding the 'Tutorialspoint' using rindex
print(string.rindex('Python', 0, 15))

When we run above program, it produces following result −

Traceback (most recent call last):
  File "main.py", line 4, in 
print(string.rindex('Python', 0, 15))
ValueError: substring not found
python_strings.htm
Advertisements