Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Full lesson preview

Count word frequencies in a text

Parse a text and count how often each word appears, normalizing case and ignoring punctuation.

Python practice18 minDictionaries & SetsIntermediateLast updated March 18, 2026

Problem statement

Given an input string, return a dictionary where keys are words (normalized to lowercase) and values are counts of how many times each word appears. Words should be sequences of alphanumeric characters and underscores (use word boundaries). Punctuation and other non-word characters should be ignored. Preserve the order of first appearance of each distinct word in the text when constructing the resulting dictionary.

Task

Create a function that returns a dictionary mapping words to their occurrence counts in a given text. Treat words case-insensitively and ignore punctuation.

Examples

Simple case-insensitive count

Input

count_word_frequencies('Hello world hello')

Output

{'hello': 2, 'world': 1}

The function lowercases words and counts occurrences: 'hello' appears twice, 'world' once. The first appearance order is 'hello', then 'world'.

Input format

A single string containing the text to analyze. Example: "This is a test."

Output format

A dictionary mapping normalized lowercase words to integer counts.

Constraints

- Use only Python standard library. - Treat words as sequences matched by the regex \b\w+\b (letters, digits, underscores). - Return an empty dictionary for empty or punctuation-only strings. - Preserve first-seen word insertion order in the result.

Samples

Sample 1

Input

count_word_frequencies("It's a test. A test!")

Output

{'it': 1, 's': 1, 'a': 2, 'test': 2}

Tokenization splits "It's" into 'it' and 's' using the word regex; all words are lowercased and counted.