计算字符串中字符出现次数的最简单方法是什么?
例如,计算'a'出现在其中的次数'Mary had a little lamb'
'a'
'Mary had a little lamb'
str.count(sub [,start [,end]])
返回sub范围中的子字符串不重叠的次数[start, end]。可选参数start,end并以片表示法解释。
[start, end]
start,end
>>> sentence = 'Mary had a little lamb' >>> sentence.count('a') 4