In [17]: info = 'my name is insaneloafer'
In [18]: print(info.count( 'a' )
2
count的注意事项
如果查询的成员(元素)不存在,则返回0
test_str = 'my name is insane'
count = test_str.count('z')
print(count) -> 0
实战
# coding:utf-8
info ="""
Open source software is made better when users can easily
contribute code and documentation to fix bugs and add features.
Python strongly encourages community involvement in improving the software.
Learn more about how to make Python better for everyone.
Contribute to Python Bug Tracker
"""
a = info.count('a')
b = info.count('b')
c = info.count('c')
d = info.count('d')
e = info.count('e')
f = info.count('f')print(a, b, c, d, e, f)
number_list =[a, b, c, d, e, f]print(number_list)print('在列表中,最大的数值是:',max(number_list))
number_dict ={'a': a,'b': b,'c': c,'d': d,'e': e,'f': f
}print('每个成员对应的数值是:', number_dict)
目录count的功能count的用法count的注意事项count的功能返回当前字符串中某个成员 (元素) 的个数count的用法用法:inttype = string.count(item)参数:item 查询个数的元素In [17]: info = 'my name is insaneloafer'In [18]: print(info.count( 'a' )2count的注意事项如果查询的成员(元素)不存在,则返回0test_str = 'my name