← All Class 11 programs

Character frequency with a dictionary

Class 11 Computer Science · Dictionary · Write the program · 3 marks

  1. Read the question and the sample tests.
  2. Write your Python program in the editor.
  3. Run Code to check it on the sample tests (no marks).
  4. Submit Answer for evaluation: hidden tests award the marks. Nothing is saved here.

Question

Write a program that reads a line of text and uses a dictionary to count how many times each character occurs, ignoring spaces. Print each character with its count in the order in which the characters first appear. Capital and small letters are different characters. Input: one line of text. Output: one line for each different character (other than space), in the form character: count

Sample tests

Use these examples to check your program. Your output must match the Expected Output exactly.

Sample 1
Sample Input
banana
Expected Output
b: 1
a: 3
n: 2
Sample 2
Sample Input
a b a
Expected Output
a: 2
b: 1

Evaluation: when you submit, your program is also checked against 3 hidden tests that you cannot see. Marks come from the hidden tests only. Each test you pass earns its marks, so a partly correct program gets part marks. Prompt text passed to input() is not counted as output.

Your program

Write the complete program. It must read any input itself and print the result.

Write the whole program, including any import lines it needs. The editor starts empty; Reset Code clears it. Submit your answer before leaving the page.

Loading Python…
Approach (open it after you have tried)

freq.get(ch, 0) returns the current count, or 0 for a character seen for the first time, so freq[ch] = freq.get(ch, 0) + 1 both adds new keys and updates existing ones. A dictionary keeps its keys in the order they were added, so traversing it prints the characters in order of first appearance.

Practise all 65 Class 11 programs

This page has 10 programs. The complete Class 11 Python Lab has 65, and keeps your best score and chapter progress.

Other Class 11 programs: Simple interest · Grade from marks · Prime or composite · Alternating series · Right triangle of stars · Palindrome string · Linear search in a list · Search in a tuple · Circle with the math module

CodeBuddy
Hi there! 👋 I'm CodeBuddy, your KV Coders assistant.

To help me give you the most accurate answers, please select your class and subject below, or just type your question!
Buddy is typing...