What's the simplest way to count the number of occurrences of a character in a string?
e.g. count the number of times 'a'
appears in 'Mary had a little lamb'
What's the simplest way to count the number of occurrences of a character in a string?
e.g. count the number of times 'a'
appears in 'Mary had a little lamb'
Regular expressions maybe?
import re
my_string = "Mary had a little lamb"
len(re.findall("a", my_string))
str.count(sub[, start[, end]])
>>> a='Mary had a little lamb'
>>> a.count('a')
4