Detailed Explanation of Python rindex of Method Case

  • 2021-11-30 00:36:44
  • OfStack

Describe

The Python rindex () method returns the index position of the last substring occurrence in the string, similar to the rfind () method, except that an exception is reported if the substring is not in the string.

Grammar

rindex () method syntax:


S.rindex(sub[,start=0[,end=len(S)]])

Parameter

sub--Specifies the substring to retrieve S--Parent string start--Optional, location to start lookup, default to 0. (Can be specified separately) end--Optional, ending the lookup position, defaults to the length of the string. (Cannot be specified separately)

Return value

Returns the index position of the last substring occurrence in the string, and reports an exception if there is no match.

Instances

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


#!/usr/bin/python3
S1 = "this is really a string example....wow!!!"
S2 = "is"
 
print (S1.rindex(S2))
print (S1.rindex(S2,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


Related articles: