find() Method to retrieve whether a string contains a target string , If you include , The index of the first occurrence of the string is returned ; conversely , Then return to -1.
find() The syntax of the method is as follows :
str.find(sub[,start[,end]])
The meanings of parameters in this format are as follows :
【 example 1】 use find() Method retrieval “c.biancheng.net” First time in “.” Location index of .
>>> str = "c.biancheng.net"
>>> str.find('.')
1
【 example 2】 Manually specify the location of the starting index .
>>> str = "c.biancheng.net"
>>> str.find('.',2)
11
【 example 3】 Manually specify the location of the start index and end index .
>>> str = "c.biancheng.net"
>>> str.find('.',2,-4)
-1
At index (2,-4) The string between is “biancheng”, Because it does not contain “.”, therefore find() The return value of the method is -1.
Be careful ,Python It also provides rfind() Method , And find() The biggest difference in the method is ,rfind() Is to retrieve from the right side of the string . for example :
>>> str = "c.biancheng.net"
>>> str.rfind('.')
11