Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python3 rindex() Method

Python3 rindex() Method

Python3 Strings Python3 Strings


Description

rindex() returns the position of the last occurrence of the substring str in a string, and raises an exception if no match is found. You can specify the optional [beg:end] parameters to set the search range.

Syntax

Syntax of the rindex() method:


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

Parameters

Return Value

Returns the position of the last occurrence of the substring str in a string, and raises an exception if no match is found.

Example

The following example shows how to use the rindex() function:


#!/usr/bin/python3
str1 = "this is really a string example....wow!!!"
str2 = "is"

print (str1.rindex(str2))
print (str1.rindex(str2,10))

The output of the above example is:


5
Traceback (most recent call last):
  File "test.py", line 6, in 
    print (str1.rindex(str2,10))
ValueError: substring not found

Python3 Strings Python3 Strings