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

1.count的功能

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

2.count的用法

592a5ef1a35abf23a6486f52d0a9c907.png

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

info = 'my name is xiaobian'
print(info.count('e'))

运行结果: 1

3.count的注意事项

  • 1)如果查询的成员(元素)不存在,则返回0。

test_str = 'my name is xiaobian'
count = test_str.count('f')
print(count)

运行结果: 0

4.代码

# 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)

运行结果:

/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
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Python中提供了很多字符串内置函数,这里列举几个比较常用的: 1. `len(str)`:返回字符串的长度。 ```python str = "hello, world!" print(len(str)) # 输出:13 ``` 2. `str.upper()`和`str.lower()`:将字符串分别转化为大写和小写形式。 ```python str = "Hello, WoRlD!" print(str.upper()) # 输出:HELLO, WORLD! print(str.lower()) # 输出:hello, world! ``` 3. `str.capitalize()`和`str.title()`:将字符串的首字母或每个单词的首字母转化为大写。 ```python str = "hello, world!" print(str.capitalize()) # 输出:Hello, world! print(str.title()) # 输出:Hello, World! ``` 4. `str.find(sub, start, end)`和`str.index(sub, start, end)`:返回子字符串在原字符串中的位置,若没有则返回-1或抛出异常。 ```python str = "hello, world!" print(str.find('o')) # 输出:4 print(str.index('o')) # 输出:4 print(str.find('z')) # 输出:-1 # print(str.index('z')) # 抛出异常:ValueError: substring not found ``` 5. `str.count(sub, start, end)`:返回子字符串在原字符串中出现的次数。 ```python str = "hello, world!" print(str.count('o')) # 输出:2 ``` 6. `str.replace(old, new, count)`:将字符串中的所有旧子字符串替换为新子字符串count为替换次数,可省略,表示替换所有。 ```python str = "hello, world!" print(str.replace('l', 'L')) # 输出:heLLo, worLd! ``` 除此之外,还有很多其他的字符串内置函数,比如`str.startswith(prefix, start, end)`、`str.endswith(suffix, start, end)`、`str.strip(chars)`、`str.join(iterable)`等等。这些函数都有其特定的功能和用法,可以根据实际情况进行选择和使用。 引用:Python字符串内置函数功能与用法总结。主要介绍了Python字符串内置函数功能与用法,结合实例形式总结分析了Python常见字符串操作函数的功能、分类、使用方法及相关操作注意事项,需要的朋友可以参考下[^1]。 引用:python string内置函数表格。string.replace(str1, str2, num=string.count(str1)) [^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

清菡软件测试

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值