"Author's Homepage": Shibie Sanri wyx
"Author's Profile": CSDN top100, Alibaba Cloud Blog Expert, Huawei CloudShare Expert, High-quality Creator in the Network Security Field
"Recommended Column": Friends who are interested in network security can pay attention to the column "Introduction to Mastery of Network Security"
count
count() is a built-in function of Python, which can " count" the specified "character" in the string or the "number of times" that the specified string appears .
grammar
String.count( str, start, end )
parameter
- str : (required) specified character or string
- start : (optional, integer) start index, default 0
- end : (optional, integer) the end index, the default last position
return value
- int type, returns the number of occurrences
Example: Count the number of occurrences of the character 'a' in the string 'abcabcabc'
print('abcabcabc'.count('a'))
output:
3
When searching in a string, the search content can only be "string type" , otherwise an error will be reported TypeError: must be str
1. Specify the search location
The start and end parameters can set the "range" of the search , including the head but not the tail[start, end)
The start parameter must be "integer" or "not set" , the default value is 0
The end parameter must be "integer" or "not set" , the default value is len(string), the length of the string, which is the last position
The parameters of count() are not specified by parameter names:
- If only "one parameter" is given , it will be assigned to the str parameter by default
- If only "two parameters" are given , assign them to the str and start parameters in order
- If "three parameters" are given , assign values to str, start, and end parameters in order
Example: Starting from the third character, search for the number of occurrences of the character 'a' in the string 'abcabcabc'
print('abcabcabc'.count('a', 3))
output:
2
Example: In the 3~6 positions of the string, search for the number of occurrences of the character 'a' in the string 'abcabcabc'
print('abcabcabc'.count('a', 3, 6))
output:
1
2. The parameter is a negative number
The start and end parameters can be "negative numbers" , which also include the head but not the tail[-start, -end)
When start is a negative number, it means "start" from the "right" position . This position is not an index, and it starts counting from 1.
print('aaabbbccc'.count('a', -6))
print('aaabbbccc'.count('a', -7))
output:
0
1
When end is a negative number, it means "end" from the "right" position , and it also starts counting from 1.
For example, in the following case, the setting range starts from the 6th on the right and ends at the 1st on the right
print('aaabbbccc'.count('c', -6, -1))
output:
2
When the negative number exceeds the "length" of the string , it will start counting from 0.
For example, the string has only 8 characters, but the value of the start parameter is -10, count() will make it "invalid".
print('aaabbbccc'.count('c', -10))
print('aaabbbccc'.count('c'))
output:
3
3
3. The count() of the list
The list also has count() , which can count the "number of times" that the specified "element" appears in the list . Although the function name is the same, the usage is different
grammar
list.count( element )
parameter
- element : the element that needs to be counted
return value
- int type, returns the number of occurrences of the element
Example: Count the number of times element 1 appears in the list
print([1, 2, 1, 2].count(1))
output:
2
4. Yuanzu's count()
The tuple is equivalent to a read-only list, and the count() of the tuple is the same as the count() of the list
grammar
tuple.count( element )
parameter
- element : the element that needs to be counted
return value
- int type, returns the number of occurrences of the element
Example: count the number of occurrences of element 1 in the tuple
print((1, 2, 1, 2).count(1))
output:
2
5. count() of other data types
The count() syntax of different data types is different:
The count() of strings, byte strings, and byte arrays can specify the start and end positions; the
count() of lists, tuples, and arithmetic sequences can only search for specified elements.
- String:
string.count( str, start, end )
- Byte string:
bytes.count( str, start, end )
- byte array:
bytearray.count( str, start, end )
- list:
list.count( element )
- tuple:
tuple.count( element )
- Arithmetic progression:
range.count( element )
6. Boolean type
The Boolean type is special. True can match 1, and False can match 0. For details, please refer to the following cases:
print((True, False).count(True))
print((True, False).count(1))
print((True, False).count(0))
output:
1
1
1