pythonrindex()方法返回字符串中最后出现的子串的索引位置。该方法与rfind()方法相同,只是如果子串不在字符串中,则会报告一个异常。
1.语法
-
rindex()方法的语法
parent_sting.rindex(sub_string[,start=0[,end=len(S)]]) -
参数
sub_string -- Specifies the substring to retrieve. parent_string -- Parent string. start -- Optional parameter. The location to start searching. The default value is 0. (can be specified separately) end -- Optional parameter. End search position. The default is the length of the string. (cannot be specified separately) -
返回值。返回子串在字符串中最后出现的索引位置。如果没有匹配,将报告一个异常。
2.Python rindex() 示例
-
下面的例子展示了如何使用rindex()方法
>>> str_1 = "I love python" >>> >>> # define the second string. >>> str_2 = "love" >>> >>> print (str_1.rindex(str_2)) 2 >>> print (str_1.rindex(str_2,10)) Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: substring not found