Analysis of knowledge points of count function in python
- 2021-08-28 20:42:22
- OfStack
In python, the count function is used to calculate the quantity in python. The count function is used to count the number of times a character appears in a string, list or Yuan Zu. It is a very useful statistical function. Please see this article for details.
1. count function
Count the number of occurrences of value element in list ls
2. Grammar
str.count("char", start,end)
Or
str.count("char") -> int Returns an integer
3. Parameters
str-is the character to be counted (either single or multi-character).
star--is the starting position of the index string, and the default parameter is 0.
end--End of index string, default parameter is string length len (str).
4. Return value
Returns the number of occurrences of statistical parameters
5. Examples
Number of times of an element in list
list = [10, 20, 30, 'Hello', 10, 20]
print "list.count('Hello') : ", list.count('Hello')
print "list.count(10) : ", list.count(10)
Output
list.count('Hello') : 1
list.count(10) : 2
Instance extension:
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 of the above example is as follows:
str.count(sub, 4, 40) : 2
str.count(sub) : 1