gpt4 book ai didi

python - 计算字符串中某个字符出现的次数

转载 作者:IT老高 更新时间:2023-10-28 11:58:37 27 4
gpt4 key购买 nike

如何计算字符串中某个字符出现的次数?

例如'a' 出现在 'Mary had a little lamb' 4 次。

最佳答案

str.count(sub[, start[, end]])

Return the number of non-overlapping occurrences of substring sub in the range [start, end]. Optional arguments start and end are interpreted as in slice notation.

>>> sentence = 'Mary had a little lamb'
>>> sentence.count('a')
4

关于python - 计算字符串中某个字符出现的次数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1155617/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com