Same as find() The method is similar to ,index() Method can also be used to retrieve whether the specified string is included , The difference is , When the specified string does not exist ,index() The method throws an exception .
index() The syntax of the method is as follows :
str.index(sub[,start[,end]])
The meanings of parameters in this format are :
【 example 1】 use index() Method retrieval “c.biancheng.net” First time in “.” Location index of .
>>> str = "c.biancheng.net"
>>> str.index('.')
1
【 example 2】 When retrieval fails ,index() It throws an exception .
>>> str = "c.biancheng.net"
>>> str.index('z')
Traceback (most recent call last):
File "<pyshell#49>", line 1, in <module>
str.index('z')
ValueError: substring not found
Same as find() and rfind() equally , String variables also have rindex() Method , Its function and index() The method is similar to , The difference is that it searches from the right , for example :
>>> str = "c.biancheng.net"
>>> str.rindex('.')
11
author : Empty bad uncle Blog
Catalog Comprehensive evaluat