Recipe
Normalize text before comparing it
Use when: text typed by a person, or read from a file, has to be compared, searched, or counted
Goal: a cleaned copy that treats spacing, accents and case as unimportant, with the original kept for display
Taught in Text-processing patterns
Quick recipe
-
1
s.strip()removes spaces, tabs and line breaks from both ends.Example
" Ada LOVELACE \n".strip()is'Ada LOVELACE'.Why: stray whitespace at the edges is the most common difference between two "identical" answers.
-
2
" ".join(s.split())turns every run of whitespace into one space.Example
" ".join("Ada LOVELACE".split())is'Ada LOVELACE'.Why:
split()with no argument throws away every extra space, andjoinputs single spaces back. -
3
unicodedata.normalize("NFC", s)(afterimport unicodedata) combines letters and accent marks into one code point where one exists.Why: the same word can arrive with
éas one code point or two (section 6), and the two never compare equal. -
4
s.casefold()on the copy you compare; keep the original for showing.Example
"Ada LOVELACE".casefold()is'ada lovelace'.Why: casefold() handles letters like
ßthat lower() leaves alone, and showing a person their own text unchanged is polite.