Python

 

Introduction

In programming, it is often necessary to analyze the frequency of each character within a string.
This can be useful in various applications such as text analysis, data processing, and even in certain algorithms
where counting occurrences of elements is required. A Frequency Counter is a tool that helps us count how many
times each character appears in a given string.

The objective of this program is to create a frequency counter that will scan through a given string, count the
frequency of each character, and output the result. The program will display each unique character alongside its
respective count, helping us understand how many times a character appears in the string.

Code: Python Program for Frequency Counter

# Frequency Counter Program in Python

# Function to count the frequency of each character in a string
def frequency_counter(input_string):
    # Creating an empty dictionary to store character frequencies
    frequency_dict = {}

    # Looping through each character in the string
    for char in input_string:
        # If the character is already in the dictionary, increment its count
        if char in frequency_dict:
            frequency_dict[char] += 1
        else:
            # If the character is not in the dictionary, add it with a count of 1
            frequency_dict[char] = 1

    # Returning the frequency dictionary
    return frequency_dict

# Input: String to analyze
input_string = "hello world"

# Calling the frequency_counter function and storing the result
result = frequency_counter(input_string)

# Printing the frequency of each character in the string
print("Character frequencies in the string:")
for char, count in result.items():
    print(f"'{char}': {count}")

Explanation of Program Structure

The program starts by defining a function called frequency_counter(input_string), which accepts a string as input.
Inside the function, we create an empty dictionary frequency_dict that will store the frequency of each character in the string.

The function uses a for loop to iterate through each character in the given string. If the character is already present
in the dictionary, its count is incremented by one. If the character is not yet in the dictionary, it is added with an initial count of one.
After the loop finishes, the function returns the dictionary containing the frequency of each character.

The program then prints out the frequency of each character in the string. The for loop iterates over the dictionary
and displays each character along with its frequency.

How to Run the Program

  1. Ensure you have Python installed on your computer.
  2. Open a text editor or Integrated Development Environment (IDE) of your choice (such as VS Code, PyCharm, or even Notepad).
  3. Copy and paste the provided Python code into a new file and save it with a .py extension (for example, frequency_counter.py).
  4. Open a terminal or command prompt window.
  5. Navigate to the directory where you saved the Python file.
  6. Run the program by typing the following command in the terminal:
    python frequency_counter.py
  7. The program will output the frequency of each character in the string you provided.
© 2025 Learn Programming. All rights reserved.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

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

error

Enjoy this blog? Please spread the word :)