擅长:python、mysql、java
<p>正如其他答案所说,使用字符串方法count()可能是最简单的,但是如果您经常这样做,请查看<a href="https://docs.python.org/2/library/collections.html#collections.Counter" rel="noreferrer">collections.Counter</a>:</p>
<pre><code>from collections import Counter
my_str = "Mary had a little lamb"
counter = Counter(my_str)
print counter['a']
</code></pre>