Text tools

A few small tools for strings.

The string module has ready-made constants: string.ascii_lowercase, string.digits, string.punctuation. No need to type out the alphabet.

str.translate swaps or deletes characters in one pass. Build the table with str.maketrans. str.maketrans("", "", string.punctuation) makes a table that deletes all punctuation, a handy cleanup step before counting words.

textwrap.fill(text, width) wraps a paragraph to a line width, and textwrap.shorten cuts it down with a placeholder. textwrap.dedent removes common leading spaces from a block, which helps with indented multi-line strings.

Run it, then use the punctuation table to fix the word count from the Collections chapter so "cat," and "cat" count as one word.