程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python find() method

編輯:Python

List of articles

  • Python find() Method : Detects whether a string contains a substring


Python find() Method : Detects whether a string contains a substring

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 :

  • str: Represents the original string ;
  • sub: Represents the target string to retrieve ;
  • start: Indicates where to start the search . If you don't specify , By default, search from the beginning ;
  • end: Indicates the end position of the end Retrieval . If you don't specify , The default is to search until the end .

【 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

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved