Python Program to Print the Frequency of each word in S

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.

Explanation

For example, if the given sentence is “Hello world, welcome to python world”, the output should be

Hello: 1
world: 2
welcome: 1
to:1
python: 1

Sample Input 1
This is my book

Sample Output 1
This: 1
is 1
my: 1
book: 1

Code:

s=input()
string=s.split()
uniques = []
for word in string:
    if word not in uniques:
        uniques.append(word)

for i in uniques:
        count=string.count(i)
        print("{}: {}".format(i,count))

Input
boys will be boys

Output
boys: 2
will: 1
be: 1

Related Posts

Python Program to Convert Integer into a Single Numeral

Single Digit Number Ram is given a positive integer N .He wishes to convert this integer into a single numeral. He does so by repeatedly adding the…

Python Program to Print Armstrong numbers between two intervals

Write a program to print all the Armstrong numbers in the given range A to B (including A and B). An N – digit number is an…

Python Program to Check if Given Input is a Letter, Digit or Special Character

Letter, Digit or Special Character You are given a character as input. Check if the given input is a Lowercase Letter or Uppercase Letter or Digit or…

Interleave Strings | Python Program to merge two strings by adding characters in alternating order

Interleave Strings Given two strings, write a program to merge the given two strings by adding characters in alternating order, starting with the first string. If a…

Python Program to Check if given colors are present in the color palate

Shekhar is an artist. He likes to draw and paint. He is now drawing a portrait of a city road which is in the black color and…

Python Program to Check the eligibility criteria for donor  

Python Comparison Operators An XYZ hospital has organised a blood donation camp. The eligibility criteria for donor was as follows  1) Age>=18  2) Weight >=50 kg  3)…

Leave a Reply

Your email address will not be published.