Given below is a Python program that takes a string as input from the user and prints the number of times each character appears in the string. You can assume that the user enters only valid characters and the string is not empty. Fill in the blanks in the following code to complete the program.

# A function that returns a dictionary that maps each character to its frequency in a string

def char_frequency(string):

    # Initialize an empty dictionary

    freq = {}

    # Loop through each character in the string

    for char in string:

        # Check if the character is already in the dictionary

        if _______________# Missing statement 1

            # Increment the value of the character by 1

            _______________# Missing statement 2

        else:

            # Add the character as a key and 1 as its value to the dictionary

            _______________# Missing statement 3

    # Return the dictionary

    _________________# Missing statement 4

# Take a string as input from the user

string = input("Enter a string: ")

# Print the number of times each character appears in the string using the char_frequency function

print("The number of times each character appears in the string is", char_frequency(string))

Answer:

Answer by student

def char_frequency(string):

    freq = {}

    for char in string:

        # Check if the character is already in the dictionary

        if char in freq: # Missing statement 1

            # Increment the value of the character by 1

            freq[char] += 1 # Missing statement 2

        else:

            # Add the character as a key and 1 as its value to the dictionary

            freq[char] = 1 # Missing statement 3

    # Return the dictionary

    return freq # Missing statement 4

string = input("Enter a string: ")

print("The number of times each character appears in the string is", char_frequency(string))

Detailed answer by teachoo

  • The char_frequency function takes a string as an argument and returns a dictionary that maps each character to its frequency in the string.
  • To create an empty dictionary, we use freq = {} .
  • To loop through each character in the string, we use for char in string: . This will assign each character to the variable char one by one.
  • To check if a character is already in the dictionary, we use if char in freq: . This will return True if char is a key in freq , False otherwise. This will be statement 1.
  • To increment the value of a character by 1, we use freq[char] += 1 . This is equivalent to freq[char] = freq[char] + 1 . This will add 1 to the current value of freq[char] and assign it back to freq[char] . This will be statement 2.
  • To add a character as a key and 1 as its value to the dictionary, we use freq[char] = 1 . This will create a new entry in freq with char as the key and 1 as the value. This will be statement 3.
  • To return the dictionary, we use return freq . This will exit the function and send back freq as the output. This will be statement 4.
  • To take a string as input from the user, we use string = input("Enter a string: ") . This will display "Enter a string: " on the screen and wait for the user to enter some text. The text entered by the user will be stored as a string in the variable string .
  • To print the number of times each character appears in the string using the char_frequency function, we use print(“The number of times each character appears in the string is”, char_frequency(string)) . This will call the char_frequency function with string as an argument and print its return value along with some text.

So, the final code is:

# A function that returns a dictionary that maps each character to its frequency in a string

def char_frequency(string):

    # Initialize an empty dictionary

    freq = {}

    # Loop through each character in the string

    for char in string:

        # Check if the character is already in the dictionary

        if char in freq: # Missing statement 1

            # Increment the value of the character by 1

            freq[char] += 1 # Missing statement 2

        else:

            # Add the character as a key and 1 as its value to the dictionary

            freq[char] = 1 # Missing statement 3

    # Return the dictionary

    return freq # Missing statement 4

# Take a string as input from the user

string = input("Enter a string: ")

# Print the number of times each character appears in the string using the char_frequency function

print("The number of times each character appears in the string is", char_frequency(string))

Go Ad-free
Davneet Singh's photo - Co-founder, Teachoo

Made by

Davneet Singh

Davneet Singh has done his B.Tech from Indian Institute of Technology, Kanpur. He has been teaching from the past 14 years. He provides courses for Maths, Science, Social Science, Physics, Chemistry, Computer Science at Teachoo.