Home >Backend Development >Python Tutorial >How to use the count function in python
Usage of count function in python
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.
count() method syntax:
str.count(sub, start= 0,end=len(string))
Parameters
sub -- Search substring
start -- Character The position of the string to start searching for. 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.
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)
The output result of the above example is as follows:
str.count(sub, 4, 40) : 2 str.count(sub) : 1
Recommended learning: Python video tutorial
The above is the detailed content of How to use the count function in python. For more information, please follow other related articles on the PHP Chinese website!