0% found this document useful (0 votes)
245 views2 pages

4 Read A Multi-Digit Number (As Chars) From The Console

Uploaded by

dishanaidu575
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
245 views2 pages

4 Read A Multi-Digit Number (As Chars) From The Console

Uploaded by

dishanaidu575
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

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

message = input('Enter your input: ')

count = {}

for character in message:

count.setdefault(character, 0)

count[character] = count[character] + 1

print('Frequency of characters in the input:\n')

pprint.pprint(count)

print()

for k, v in count.items():

print(f"Character '{k}' appeared {v} times")


Output:
Enter your input:
292749028574908279082574258979529847209846987620234456025657260974502874597264652
Frequency of words appeared in the Sentence or paragraph
{'0': 8,
'2': 15,
'3': 1,
'4': 10,
'5': 10,
'6': 7,
'7': 11,
'8': 8,
'9': 11}

Character '2' appeared 15 times


Character '9' appeared 11 times
Character '7' appeared 11 times
Character '4' appeared 10 times
Character '0' appeared 8 times
Character '8' appeared 8 times
Character '5' appeared 10 times
Character '6' appeared 7 times
Character '3' appeared 1 times

You might also like