Detailed explanation of the usage of count function in python

angryTom
Release: 2020-02-05 10:44:22
Original
35877 people have browsed it

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"))  #多字符统计
Copy after login

Program running results:

3
2
0
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template