Answer to Question #292751 in Python for Hamza Zaidi

Question #292751

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


1
Expert's answer
2022-02-01T05:54:14-0500
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])))

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS