Latest web development tutorials

Python3 rindex () method

Python3 string Python3 string


description

rindex () returns the position of substring in string str the last occurrence, if no matching string reported abnormal, you can specify the optional parameter [beg: end] with discovery interval.

grammar

rindex () method syntax:

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

parameter

  • str - the search string
  • beg - Find the location, the default is 0
  • end - the end find a location, the default is the length of the string.

return value

Returns the position of substring in string str the last occurrence, if no matching string reported abnormal.

Examples

The following example shows rindex () function to use:

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

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

Examples of the above output results are 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 string Python3 string