Python Program to Count the Frequency of Each Word in a String using Dictionary

This Python program counts the frequency of each word in a string using a dictionary.ch.

Problem Statement

Write a Python program to count the frequency of each word in a given string. Your program should use a dictionary to store the word frequencies and provide the count for each unique word in the string. The program should be case-insensitive and should not consider common punctuation marks while identifying words.

Python Program to Count the Frequency of Each Word in a String using Dictionary

def count_word_frequency(input_string):
    # Removing punctuation and converting the string to lowercase
    cleaned_string = input_string.lower().replace('.', '').replace(',', '').replace('!', '').replace('?', '')
    
    # Splitting the string into words
    words = cleaned_string.split()
    
    # Creating an empty dictionary to store word frequencies
    word_frequency = {}
    
    # Counting word frequencies
    for word in words:
        if word in word_frequency:
            word_frequency[word] += 1
        else:
            word_frequency[word] = 1
    
    return word_frequency

# Input string
input_string = "This is a sample string, and this is a simple program. Is this a sample or a simple string?"

# Counting word frequency
frequency_dict = count_word_frequency(input_string)

# Printing word frequencies
for word, frequency in frequency_dict.items():
    print(f"'{word}': {frequency}")

How it Works

  1. The count_word_frequency function takes an input string as an argument.
  2. The input string is first cleaned by converting it to lowercase and removing common punctuation marks (periods, commas, exclamation marks, and question marks) using the replace method.
  3. The cleaned string is then split into individual words using the split method, creating a list called words.
  4. An empty dictionary called word_frequency is created to store the word frequencies.
  5. The program iterates through each word in the words list. If the word is already present in the word_frequency dictionary, its count is incremented. Otherwise, a new entry is added to the dictionary with the word as the key and the value set to 1.
  6. Once all the words have been processed, the word_frequency dictionary containing the word frequencies is returned.
  7. The program then uses the input_string to count word frequencies by calling the count_word_frequency function.
  8. The word frequencies are printed out using a loop that iterates over the items in the frequency_dict dictionary. Each word and its corresponding frequency are printed in the specified format.

Input/ Output

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

In this python tutorial, you will learn how to Display Prime Numbers Between Two Intervals using the if and else...
In this python tutorial, you will learn how to Calculate Standard Deviation with built in functions of the python programming...
In this Python program, we will convert temperature values from Celsius to Fahrenheit. The Celsius and Fahrenheit scales are two...