Write a program that reads string from user. Your program should create a dictionary having key as word length and value is count of words of that length. For example, if user enters 'A fat cat is on the mat'.
Word | Word length |
A | 1 |
fat | 3 |
cat | 3 |
is | 2 |
on | 2 |
the | 3 |
mat | 3 |
The content of dictionary should be {1:1, 3:4, 2:2}
Source Code
text = input('Enter a sentence: ')
words = text.split()
d = {}
for word in words:
if len(word) in d:
d[len(word)] += 1
else:
d[len(word)]=1
print(d)
Output