Pages

Python: Counting Letters in Statement

We will use dictionary to do that.

statement = 'This is a good day to live.'
dict = {}

for letter in statement:

if letter in dict:
dict[letter] = dict[letter] + 1
else:
dict[letter] = 1

print "Used letter numbers including ' ' and '.'"

for j in sorted(dict):

print j , '---->' , dict[j]

This will print: