Python program to Count Words in a String using Dictionary

string = input("Enter any String: ")
words = []
# To Avoid Case-sensitiveness of the string
words = string.lower().split()
frequency = [words.count(i) for i in words]
myDict = dict(zip(words, frequency))
print("Dictionary Items :  ",  myDict)

OUTPUT:

Enter any String: chase
Dictionary Items :   {'chase': 1}

Sharing Is Caring

Leave a Comment