Python3 rindex() Method

Python3 字符串Python3 Strings


Description

rindex() returns the position of the last occurrence of substring str in the string, or raises an exception if there is no matching string. You can specify the optional parameter [beg:end] to set the search interval.

Syntax

rindex() method syntax:

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

Parameters

  • str -- The string to search for
  • beg -- The starting position for the search, defaults to 0
  • end -- The ending position for the search, defaults to the length of the string.

Return Value

Returns the position of the last occurrence of substring str in the string, or raises an exception if there is no matching string.

Examples

The following examples demonstrate the use of the rindex() method:

#!/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 as follows:

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

Python3 字符串Python3 Strings

Other Extensions