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

Python count() method

編輯:Python

List of articles

  • Python count() Method : Count the number of string occurrences
    • count The syntax of the method is as follows :
      • 【 example 1】 Retrieve string “c.biancheng.net” in “.” Number of occurrences .
      • 【 example 2】
      • 【 example 3】


Python count() Method : Count the number of string occurrences

count Method to retrieve the number of times a specified string appears in another string , If the retrieved string does not exist , Then return to 0, Otherwise, return the number of occurrences .

count The syntax of the method is as follows :

str.count(sub[,start[,end]])

In this way , The specific meaning of each parameter is as follows :

  • str: Represents the original string ;
  • sub: Represents the string to retrieve ;
  • start: Specify the starting location of the search , That is, from where to start the detection . If you don't specify , Search from scratch by default ;
  • end: Specify the end of the retrieval , If you don't specify , It means to search all the way to the end .

【 example 1】 Retrieve string “c.biancheng.net” in “.” Number of occurrences .

>>> str = "c.biancheng.net"
>>> str.count('.')
2

【 example 2】

>>> str = "c.biancheng.net"
>>> str.count('.',1)
2
>>> str.count('.',2)
1

I talked about it before. , The retrieval value corresponding to each character in the string , from 0 Start , therefore , Retrieve values in this example 1 Corresponding to No 2 Characters ‘.’, From the output we can analyze , Retrieve from the specified index location , It also includes the index location .

【 example 3】

>>> str = "c.biancheng.net"
>>> str.count('.',2,-3)
1
>>> str.count('.',2,-4)
0

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