4 Read A Multi-Digit Number (As Chars) From The Console
4 Read A Multi-Digit Number (As Chars) From The Console
4. Read a multi-digit number (as chars) from the console. Develop a program to print the
frequency of each digit with suitable message.
ALGORITHM:
1. import pprint: Imports the pprint module, which is used for pretty-printing data structures.
2. message = input('Enter your input: '): Prompts the user to enter a string and assigns it to the
variable message.
3. count = {}: Initializes an empty dictionary to store character frequencies.
4. Character Frequency Calculation:
for character in message: iterates through each character in the input message.
count.setdefault(character, 0) initializes the count of each character to 0 if it's not
already in the dictionary.
count[character] = count[character] + 1 increments the count for each character
encountered.
5. Pretty Printing:
print('Frequency of characters in the input:\n'): Prints a message indicating that the
following output will display character frequencies.
pprint.pprint(count): Uses pprint to print the dictionary count in a more readable
format.
6. Character Count Display:
for k, v in count.items(): iterates through the key-value pairs in the count dictionary.
print(f"Character '{k}' appeared {v} times"): Prints each character along with its
frequency in a formatted string using f-string notation.
This code takes an input string, counts the frequency of each character, pretty-prints the frequency
dictionary, and then displays each character along with its count.
CODE:
import pprint
count = {}
count.setdefault(character, 0)
count[character] = count[character] + 1
pprint.pprint(count)
print()
for k, v in count.items():