计算字符串中字符的出现次数 [英] Count the number occurrences of a character in a string
本文介绍了计算字符串中字符的出现次数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
计算字符串中字符出现次数的最简单方法是什么?
What's the simplest way to count the number of occurrences of a character in a string?
例如计算'a'
在'Mary有点羊羔' $ p $中出现的次数b
e.g. count the number of times 'a'
appears in 'Mary had a little lamb'
推荐答案
返回子字符串<$ c不重叠的次数$ c> 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
这篇关于计算字符串中字符的出现次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文