PythiloLesson 30 of 30 · progress kept in this browser

Project: a word report

This lesson puts the course together. A word report reads text, cleans it into words, counts them and lists the most common ones. Split the job into small functions so each step can be tested alone.

import string
from collections import Counter

print(string.punctuation)
print("'Hi!'".strip(string.punctuation))
print("don't".strip(string.punctuation))
print(repr("...".strip(string.punctuation)))

words = ["the", "cat", "the"]
print(Counter(words).most_common(1))
Your task

Write clean_words(text) returning the lower-case words of text, with punctuation stripped from both ends of each and empty results dropped. Write report(text, n) returning the n most common words as lines like "cat: 2" joined with newlines, or "no words" when there are none.

All lessons