Home  >  Article  >  Backend Development  >  Detailed explanation of the usage of count function in python

Detailed explanation of the usage of count function in python

angryTom
angryTomOriginal
2020-02-05 10:44:2235771browse

Detailed explanation of the usage of count function in python

Usage of count function in python

count() function

Description: Statistical characters The number of times a certain character appears in a string, you can select the starting position and ending position of the string index.

Syntax: str.count("char", start,end) or str.count("char")

Return value: Integer

Parameter description:

str —— Character to be counted (can be a single character or multiple characters)

star —— Index character The starting position of the string, the default parameter is 0

end - the end position of the index string, the default parameter is the length of the string, that is, len(str)

Program example:

str = "i love python,i am learning python"
print(str.count("i")) #star 和end 为默认参数
print(str.count("i",2)) # star值为2,end值为默认参数
print(str.count("i",2,5)) #star值为2,end值为5
print(str.count("am"))  #多字符统计

Program running results:

3
2
0

Recommended learning: Python tutorial

The above is the detailed content of Detailed explanation of the usage of count function in python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn