Word Counter
Easy
22
1
60.5% Acceptance
In this lab, you will create a Python function count_words
that takes a string as input and returns a dictionary with the count of each unique word. The function should ignore case and punctuation.
Function Requirements:
- The function must be named
count_words
. - It should accept a single argument: a string of text.
- The returned dictionary should have words as keys and their counts as values.
- Words in the dictionary are case-insensitive ('Word' and 'word' are considered the same).
- Punctuation should be ignored (e.g., 'hello!' and 'hello' are the same word).
Examples:
-
Input:
"Hello world! Hello again, world."
Output:
{"hello": 2, "world": 2, "again": 1}
-
Input:
"Unique words only once."
Output:
{"unique": 1, "words": 1, "only": 1, "once": 1}
Edge Cases to Consider:
- A word that appears in both uppercase and lowercase in the text should be counted as a single word with the sum of both occurrences.
- Any punctuation attached to words should not affect the count.
Ensure your function handles these cases correctly to pass all the challenges in the lab.