Pages

Python: Counting Words in Statement

We can use dictionaries for word count too.

text = 'Python is a programming language that lets you work more quickly and integrate your systems more effectively. You can learn to use Python and see almost immediate gains in productivity and lower maintenance costs.'

word = text.split()

dict = {}

for i in word:

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

for j in sorted(dict):

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

This will print: