Skip to main content
  • Python is available now. All other compilers are coming soon.

    Python
  • Popular Tutorials

    • Getting Started With Python
    • Python if Statement
    • while Loop in Python
    • Python Lists
    • Dictionaries in Python
    All Python Tutorials

    Reference Materials

    Built-in FunctionsList MethodsDictionary MethodsString Methods
    View all

Online Compilers

  • Python Compiler
  • Python Playground
  • Python Projects
  • Exercises

Tutorials

  • Learn Python
  • Python for Beginners
  • Variables and Types
  • Python Strings
  • Python Lists
  • Python If Else
  • Python For Loops
  • Python Functions
  • Python Classes

Examples

  • Python Examples
  • Hello World
  • Add Two Numbers
  • Even or Odd
  • Factorial
  • Fibonacci
  • Prime Check
  • Palindrome Check

Company

  • About
  • Contact
  • Privacy Policy
  • Terms of Use
  • Disclaimer

© 2026 ILoveCompiler. All rights reserved.

  1. Home
  2. Python Exercises
  3. Word Frequency

Dictionaries and counting · Python exercise

Word Frequency

Count each word in a line and print the counts alphabetically.

intermediate3 testsdictionariescountingsorting

Problem

Read one line of text. Count each word using case-insensitive comparison, then print one line per distinct word in alphabetical order in the format word count.

Input and output

Input
One line of text.
Output
One line per distinct word: the lowercase word, a space, and its count.

Constraints

  • The line contains at most 10,000 characters.

Examples

Example 1

Input

the cat the dog the

Output

cat 1
dog 1
the 3

Example 2

Input

Hello hello

Output

hello 2

How testing works

Your code runs once per test case with that case's standard input. Output is compared after normalising trailing spaces and blank lines at the end. Use input() without a prompt when the exercise expects clean output.

Your solution3 hidden input checks

Loading editor…

Press Run tests to execute your code against every input case. Python runs in a Web Worker in your browser.

Related exercises

intermediate

Character Frequency

Count non-space characters in a line and print a sorted table.

3 tests
intermediate

Highest Score

Read names and scores, then print the highest-scoring student.

3 tests
Previous exerciseGreeting FunctionNext exerciseCharacter Frequency