Home > Backend Development > Python Tutorial > What is the function of function COUNT() in python

What is the function of function COUNT() in python

coldplay.xixi
Release: 2020-10-29 09:30:37
Original
10360 people have browsed it

The function of the function COUNT() in python is to count the number of times a certain character appears in a string. The syntax is [str.count("char", start,end)], where str is the character to be counted. , star is the starting position of the index string, and end is the end position of the index string.

What is the function of function COUNT() in python

count() function

Description: Count the number of times a certain character appears in a string. You can select the starting and ending positions of the string index.

Syntax: str.count("char", start,end) or str.count("char") -> int Returns an integer

  • str —— is the character to be counted (can be a single character or multiple characters).

  • star - is the starting position of the index string, the default parameter is 0.

  • end - is the end position of the index string. The default parameter is the string length, which 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 result:

3
2
0
1
Copy after login

Related free learning recommendations: python video tutorial

The above is the detailed content of What is the function of function COUNT() 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