Write a program that takes a sentence as input from the user, and prints the frequency of each
word in the sentence and neglects any punctuations. ‘.’,’,’,’!’ Input: Hello Amna. Amna is a good girl.
Output:
Hello: 1 Amna: 2 is: 1
a: 1 good: 1 girl:1
import re
InputString = str(input("Enter a string: "))
InputString = re.sub('[.,?!@#$]', '', InputString)
strng = InputString.split()
tempStr = []
for i in strng:
if i not in tempStr:
tempStr.append(i)
print("Word Frequency")
for i in range(0, len(tempStr)):
print("%10s:%5s"%(tempStr[i].ljust(20), strng.count(tempStr[i])))
Comments
Leave a comment