Python count () method: the number of statistical occurrence of a string

A method for retrieving count the number of times specified string in another string, if the string is retrieved does not exist, returns 0, otherwise the number of occurrences.

syntax count method is as follows:

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

In this method, the specific meaning of each parameter are as follows: https://www.furuihua.cn/guangdong/

  1. str: represents the original string;
  2. sub: Indicates the character string to be retrieved;
  3. start: Specifies the retrieval starting position, that is, from the beginning what position detection. If not specified, the default start from scratch retrieval;
  4. end: end position designated retrieval, if not specified, the end has been retrieved.


[Times] 1 case of search character string "c.biancheng.net" in. "" Appears.

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


[Example 2]

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

Recall that each retrieve the value corresponding to the character string starting from 0, therefore, the present embodiment is to retrieve the value corresponding to a second character '.', The output can be analyzed from the start retrieved from the specified index , which also contains the index position.

[Example 3]

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

Guess you like

Origin www.cnblogs.com/furuihua/p/12640673.html
Recommended