admin管理员组

文章数量:1794759

python全栈开发《21.字符串的count函数》

1.count的功能

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

2.count的用法

string代表需要处理的字符串。通过.count(item)来调用这个函数。()里的item是要被查询个数的元素。它会返回一个整型。那么inttype就是说:返回的是一个数字。

代码语言:javascript代码运行次数:0运行复制
info = 'my name is xiaobian'
print(info.count('e'))

运行结果: 1

3.count的注意事项

  • 1)如果查询的成员(元素)不存在,则返回0。
代码语言:javascript代码运行次数:0运行复制
test_str = 'my name is xiaobian'
count = test_str.count('f')
print(count)

运行结果: 0

4.代码

代码语言:javascript代码运行次数:0运行复制
# coding:utf-8

info = '''
    Python now uses the same ABI whether it’s built in release or debug mode. 
    On Unix, when Python is built in debug mode, it is now possible to load C 
    extensions built in release mode and C extensions built using the stable 
    ABI.

    Release builds and debug builds are now ABI compatible: 
    defining the Py_DEBUG macro no longer implies the Py_TRACE_REFS macro, 
    which introduces the only ABI incompatibility. The Py_TRACE_REFS macro, 
    which adds the sys.getobjects() function and the PYTHONDUMPREFS 
    environment variable, can be set using the new 
    ./configure --with-trace-refs build option. 
    (Contributed by Victor Stinner in bpo-36465.)
'''

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)

运行结果:

代码语言:javascript代码运行次数:0运行复制
/Users/llq/PycharmProjects/pythonlearn/pythonlearn/.venv/bin/python /Users/llq/PycharmProjects/pythonlearn/pythonlearn1/count.py 
20 20 14 18 54 4
[20, 20, 14, 18, 54, 4]
在列表中,最大的数值: 54
每个成员对应的数值分别是: {'a': 20, 'b': 20, 'c': 14, 'd': 18, 'e': 54, 'f': 4}

进程已结束,退出代码为 0
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。原始发表:2024-07-28,如有侵权请联系 cloudcommunity@tencent 删除函数开发全栈字符串count

本文标签: python全栈开发《21字符串的count函数》