Count number of each character in a string
- string
- counter
- dict
The simplest way of counting the occurences of a character in a string is to use collections.Counter
.
from collections import Counter
s = 'Look at my string, my string is amazing, give it a lick, it tastes of pixels.'
counter = Counter(s)
print(counter.most_common(5))
# [(' ', 15), ('i', 9), ('t', 7), ('s', 6), ('a', 5)]