In today’s blog post, we’re diving into a fun and educational Python coding challenge that will test your skills in string manipulation and dictionary handling. This challenge is perfect for beginners who are looking to get more hands-on experience with Python. Let’s break down the challenge and then go through a step-by-step guide on how to solve it.

Advertisement

Objective

The goal is to write a Python program that counts the frequency of each alphabetic character in a given string and outputs the result in a dictionary format.

Requirements

  • User Input: The program should prompt the user to input a string.
  • Output: The result should be a dictionary where each key is a unique character, and the value is the number of times that character appears in the string.
  • Case Sensitivity: The program must differentiate between uppercase and lowercase letters (e.g., ‘a’ vs ‘A’).
  • Character Filtering: Ignore spaces, punctuation, and special characters—only alphabetic characters are considered.

Example

Input:


Hello World

Output:


{'H': 1, 'e': 1, 'l': 3, 'o': 2, 'W': 1, 'r': 1, 'd': 1}

Step-by-Step Solution

  • Prompt the User for Input: Use the input() function to get the string from the user.
  • Initialize a Dictionary: This will store each character and its frequency.
  • Loop Through the String: For each character, check if it’s alphabetic using char.isalpha().
  • Count the Frequency: If the character is valid, convert it to its case-sensitive form and update the dictionary.
  • Output the Result: Print the dictionary containing the character frequencies.

Implementation

Here’s a simple Python program based on the above specifications:


def count_character_frequency():
    user_input = input("Enter a string: ")
    frequency_dict = {}

    for char in user_input:
        if char.isalpha():  # Check if the character is alphabetic
            if char in frequency_dict:
                frequency_dict[char] += 1
            else:
                frequency_dict[char] = 1

    print(frequency_dict)

# Calling the function to test
count_character_frequency()

Bonus Challenge

For those who want to take an extra step, let’s modify the program to also count numeric characters.

Enhanced Requirements: Include numbers in the frequency count along with alphabetic characters.

Bonus Implementation

You only need to tweak the character validation part to include digits:


def count_character_frequency():
    user_input = input("Enter a string: ")
    frequency_dict = {}

    for char in user_input:
        if char.isalnum():  # Check if the character is alphabetic or numeric
            if char in frequency_dict:
                frequency_dict[char] += 1
            else:
                frequency_dict[char] = 1

    print(frequency_dict)

# Calling the function to test the bonus challenge
count_character_frequency()

Conclusion

This challenge helps reinforce knowledge of strings, loops, conditionals, and dictionary operations in Python. By adjusting the conditions and dictionary handling, we can easily expand this simple program to cover more complex scenarios, like including numbers or even customizing which characters to ignore. Whether you’re a beginner or looking to polish your Python skills, challenges like these are a great way to learn and have fun with coding!

Feel free to experiment with the code, and if you have any suggestions or need further modifications, don’t hesitate to ask. Happy coding!

Share.
Leave A Reply


Exit mobile version