Home > Backend Development > Python Tutorial > Detailed explanation of usage of python count function

Detailed explanation of usage of python count function

hzc
Release: 2020-07-04 14:05:25
Original
14356 people have browsed it

In python, you can use the "count()" function to count the number of times a certain character appears in a string. This function is used to count the number of times. Its syntax is "count(sub, start= 0,end=len (string))", its parameter sub represents the searched substring.

Detailed explanation of usage of python count function

#The Python count() method is used to count the number of times a certain character appears in a string. Optional parameters are the start and end positions of the string search.

Syntax

count() method syntax:

str.count(sub, start= 0,end=len(string))
Copy after login
  • sub -- Search substring

  • start -- The position where the string search starts. The default is the first character, and the index value of the first character is 0.

  • end -- The position in the string where the search ends. The first character in the string has index 0. Defaults to the last position of the string.

#!/usr/bin/python
 
str = "this is string example....wow!!!";
 
sub = "i";
print "str.count(sub, 4, 40) : ", str.count(sub, 4, 40)
sub = "wow";
print "str.count(sub) : ", str.count(sub)
Copy after login
str.count(sub, 4, 40) :  2
str.count(sub) :  1
Copy after login

Recommended tutorial: "Python Tutorial"

The above is the detailed content of Detailed explanation of usage of python count function. 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