Python字符串的count函数

目录

  • count的功能
  • count的用法
  • count的注意事项

count的功能

  • 返回当前字符串中某个成员 (元素) 的个数

count的用法

  • 用法:inttype = string.count(item)
  • 参数:item 查询个数的元素
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)
15 6 8 7 30 5
[15, 6, 8, 7, 30, 5]
在列表中,最大的数值是: 30
每个成员对应的数值是: {'a': 15, 'b': 6, 'c': 8, 'd': 7, 'e': 30, 'f': 5}

Process finished with exit code 0
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值