# Words by the Numbers 3, Exercise 1: a letter-frequency table for a short invented passage. import re from collections import Counter text = ( "The lighthouse keeper climbed the stairs every evening to light the lamp, " "and the ships that passed in the dark steered by its steady beam." ) letters = re.findall(r"[a-z]", text.lower()) counts = Counter(letters) total = len(letters) print("letters:", total, "| even-spread share:", f"{1 / 26:.1%}") for letter, n in counts.most_common(5): print(f"{letter} {n:3d} {n / total:6.1%}") print("e share:", f"{counts['e'] / total:.1%}", "| letters never used:", 26 - len(counts)) # Output when run (Python 3.14; the documentation-text figures depend on the Python version): # letters: 113 | even-spread share: 3.8% # e 20 17.7% # t 14 12.4% # h 10 8.8% # s 10 8.8% # i 8 7.1% # e share: 17.7% | letters never used: 6