Working with the string Module
A useful normalization workflow is to remove selected punctuation with translate(), convert letters to lowercase with lower(), and remove trailing whitespace with rstrip().
Why Text Needs Normalizing
Text that looks similar to a person may still be different to a program. For example, Hello and hello differ in case, while word, includes punctuation that word does not. Extra spaces at the end can also remain attached to a piece of text. If you count words or analyze text without cleaning it first, these differences can make your results inaccurate.
A useful normalization workflow is to remove selected punctuation with translate(), convert letters to lowercase with lower(), and remove trailing whitespace with rstrip().
Following a Translation Table
translate() changes characters according to a translation table. str.maketrans() creates that table. The table can specify replacement characters, and it can also mark characters for deletion by mapping them to None. This makes translate() flexible: the same general mechanism can replace selected characters or remove them.
1 2!In this example, the selected characters a and b are replaced according to the translation table. The exclamation mark is not selected for replacement, so it remains. To remove characters rather than replace them, use an empty fromstr and tostr with the characters to delete supplied separately.
Deleting Punctuation Before Counting
The string module provides string.punctuation, a predefined collection of punctuation characters. Using it avoids manually typing each punctuation mark. The expression str.maketrans('', '', string.punctuation) creates a translation table with no replacements and with punctuation marked for deletion.
import string
line = "word, word!"
table = str.maketrans('', '', string.punctuation)
cleaned = line.translate(table)
print(cleaned)- 1The line begins with punctuation attached to each word.
- 2
str.maketrans('', '', string.punctuation)creates the deletion table. - 3
translate(table)removes the punctuation while leaving the other characters. - 4The resulting text contains the two words separated by a space.
- The string module supplies
string.punctuation. - The translation table has no replacement characters and marks punctuation for deletion.
translate()applies the table to the line.
word wordOrdering the Cleaning Operations
hello helloThe first operation removes punctuation, so punctuation does not remain attached to words. The second converts uppercase letters to lowercase, so Hello and HELLO become the same form. The final operation removes trailing whitespace from the right end. Together, these operations produce more consistent text for later counting or analysis.
Controlling the Right Edge
rstrip() removes characters only from the right end of a string. Called without an argument, it removes trailing whitespace. Called with a string argument, it removes only the specified characters from the right end. Characters at the beginning and in the middle are preserved.
data
data!!The first call removes the exclamation marks at the right end because they are the specified characters. The leading spaces remain, and characters in the middle are not targeted. The second call has no argument, so it removes trailing whitespace; because this string has no trailing whitespace, its visible text is unchanged.
Mistakes in Text Cleaning
Common mistake Treating uppercase and lowercase words as different without normalizing case.
What it looks likeA counter can encounter Hello and hello as separate text forms.
Why it failsThe two forms differ in case even though the workflow may intend to count them together.
Do this insteadApply lower() before counting or comparing the text.
Common mistake Removing punctuation by manually typing a partial list of punctuation characters.
What it looks likeA deletion table omits some punctuation marks.
Why it failsThe omitted marks can remain attached to words and affect analysis.
Do this insteadUse string.punctuation when the goal is to work with the punctuation characters provided by the string module.
Common mistake Expecting rstrip() to remove unwanted characters everywhere in the string.
What it looks likeA matching character in the middle of the text remains after rstrip().
Why it failsrstrip()operates only at the right end.Do this insteadUse rstrip() specifically for right-end cleanup, not for removing matching characters from the beginning or middle.
Common mistake Using replacement mappings when deletion is required.
What it looks likeA punctuation character is assigned a replacement instead of being marked for deletion.
Why it failsThe character is changed rather than removed.
Do this insteadUse str.maketrans('', '', characters_to_delete) when the selected characters should be deleted.
Practice the Workflow
A line contains the text "Ready, READY! ". Describe the three cleaning operations that would normalize it for counting. State what the text should look like after punctuation deletion, after lower(), and after rstrip().
Hints
- Use
string.punctuationwithstr.maketrans('', '', string.punctuation)for deletion. - Apply
lower()after punctuation has been removed. - Call
rstrip()without an argument to remove trailing whitespace.
Normalizing a Short Line
Normalize the generated text "Ready, READY! " using punctuation deletion, lowercase conversion, and right-end whitespace removal.
- 1Delete punctuation
Use a translation table made with an empty fromstr and tostr and
string.punctuationas the deletion set. The text becomes "Ready READY ". - 2Convert case
Apply
lower()so the uppercase letters are converted to lowercase. The text becomes "ready ready ". - 3Remove trailing whitespace
Apply
rstrip()without an argument to remove whitespace from the right end. The text becomes "ready ready".
The normalized text is "ready ready", so both word forms use the same case and punctuation and trailing whitespace no longer interfere with processing.
Key Takeaways
- 1
str.maketrans()creates the translation table used bytranslate(). - 2A translation table can replace selected characters or map them to None for deletion.
- 3Use
string.punctuationas the predefined punctuation set when punctuation should be removed. - 4
lower()makes uppercase and lowercase forms consistent for counting and comparison. - 5
rstrip()removes unwanted characters only from the right end, and without an argument it removes trailing whitespace.
At a glance
- Use
translate()withstr.maketrans()when selected characters must be replaced or deleted. - Use
str.maketrans('', '', string.punctuation)to configure punctuation deletion. - Apply
lower()so text forms that differ only by case become consistent. - Use
rstrip()for cleanup at the right end, especially for trailing whitespace. - Combine these operations before counting or analyzing text to improve consistency.