Answer to Question #165334 in Python for hemanth

Question #165334

Word Count

Given a sentence S, write a program to print the frequency of each word in S. The output order should correspond with the word's input order of appearance.


Input


The input will be a single line containing a sentence S.


Output


The output contains multiple lines, with each line containing a word and its count separated by ": " with the order of appearance in the given sentence.


1
Expert's answer
2021-02-21T19:53:33-0500
InputString = str(input("Enter a string: "))  
str = InputString.split()
tempStr=[]
for i in str:              
    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),str.count(tempStr[i])))   



Python Output:

Enter a string: Given a sentence S, write a program to print the frequency of each word in S. The output order should correspond with the word's input order of appearance.
Word                  Frequency
Given               :    1
a                   :    2
sentence            :    1
S,                  :    1
write               :    1
program             :    1
to                  :    1
print               :    1
the                 :    2
frequency           :    1
of                  :    2
each                :    1
word                :    1
in                  :    1
S.                  :    1
The                 :    1
output              :    1
order               :    2
should              :    1
correspond          :    1
with                :    1
word's              :    1
input               :    1
appearance.         :    1




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