Home  >  Article  >  Backend Development  >  Python counts the number of characters

Python counts the number of characters

藏色散人
藏色散人Original
2020-01-13 11:27:2215737browse

Python counts the number of characters

Python counts the number of characters

Python count() method

Description

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))

Parameters

sub -- Searched Substring

start -- The position in the string where the search begins. 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.

Return value

This method returns the number of times the substring appears in the string.

Example

The following example shows an example of the count() method:

Example (Python 2.0)

#!/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)

Above example The output results are as follows:

str.count(sub, 4, 40) :  2
str.count(sub) :  1

Many python training videos, all on the python learning network, welcome to learn online!

The above is the detailed content of Python counts the number of characters. 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
Previous article:python3.8 new featuresNext article:python3.8 new features