Python3 rindex() Method
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
- str -- the string to find
- beg -- the position to start searching, defaults to 0
- end -- the position to end searching, defaults to the length of the string.
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