Sub topic 3
Case: Lowercasing Versus Case Folding
This chapter assumes you know basic Python strings, functions, and list comprehensions. It also assumes that text can contain Unicode characters and that a tokenizer turns text into tokens such as words.
Case differences often do not change the meaning of text. For example, Search, search, and SEARCH may represent the same word. A text-processing system can make these forms equal before comparison. The choice between lowercasing and case folding matters because they do not handle every language in the same way.
Lowercasing removes ordinary case differences
Python's str.lower() method converts uppercase letters to lowercase letters:
words = ["Search", "SEARCH", "Searchable"]
for word in words:
print(word.lower())The output is:
search
search
searchableLowercasing is simple and often useful for English text. It helps when you want to:
- Compare words without treating uppercase and lowercase as different.
- Count words regardless of how they appear.
- Match user input against lowercase keywords.
- Build a basic search index.
However, lowercasing is not a complete definition of “case-insensitive.” Some Unicode letters need a stronger transformation. A lowercased string can still differ from another string that a reader considers equivalent.
Case folding is designed for caseless matching
Case folding is a Unicode transformation for comparing text without regard to case. Python provides it through str.casefold().
The German letter ß, called sharp s, demonstrates the difference:
print("Straße".lower()) # straße
print("Straße".casefold()) # strasse
print("Straße".casefold() == "STRASSE".casefold()) # Truelower() preserves ß. casefold() maps it to ss, which allows Straße and STRASSE to compare as equal for caseless matching.
| Operation | Main purpose | Example result for `"Straße"` |
|---|---|---|
| `lower()` | Convert letters to lowercase | `"straße"` |
| `casefold()` | Prepare Unicode text for caseless comparison | `"strasse"` |
Case folding can change the number of characters. That is expected. The result is intended for comparison, indexing, and matching, not necessarily for display.
Case folding is not the same as lowercasing
Case folding makes some transformations that lowercasing does not. It may map one character to multiple characters, and it can remove distinctions that matter in a particular application.
For example:
text = "Straße"
print(text.lower())
print(text.casefold())Output:
straße
strasseThis means that case folding is usually the better choice when the requirement is “match these strings regardless of case.” Lowercasing may be the better choice when you need a predictable display transformation or when an existing external system specifically defines its normal form using lowercase.
Case folding does not remove accents, punctuation, extra spaces, or unrelated spelling differences. These are separate cleaning decisions:
a = "Café"
b = "CAFE"
print(a.casefold() == b.casefold()) # FalseThe comparison remains false because é and e are different letters. Accent handling requires Unicode decomposition and an explicit decision to remove combining marks. Do not assume that case folding performs accent stripping.
Apply case folding at the comparison boundary
A useful pattern is to preserve the source value and derive a comparison value:
def comparison_key(text):
return text.casefold()
original = "Straße"
key = comparison_key(original)
print(original) # Straße
print(key) # strasseUse the same transformation on both sides of a comparison:
def same_word(left, right):
return left.casefold() == right.casefold()
print(same_word("Straße", "STRASSE")) # True
print(same_word("Python", "PYTHON")) # TrueApplying the transformation consistently is more important than transforming only the input or only the stored data. If a stop-word list contains lowercase strings, case-fold the incoming tokens before testing membership, and case-fold the list when it is created if its source is not already normalised.
Combine case folding with Unicode normalisation
Case folding handles case. Unicode normalisation handles different code-point sequences that represent equivalent text. A comparison key often applies both:
import unicodedata
def comparison_key(text):
text = unicodedata.normalize("NFC", text)
return text.casefold()
left = "CAFÉ"
right = "café"
print(comparison_key(left) == comparison_key(right)) # TrueThe order can matter when a later step depends on the output of an earlier step. Keep the pipeline explicit, test it with the languages and symbols in your data, and record which transformations it applies.
Case folding is a tool for a specific purpose: Unicode-aware caseless matching. It is not a universal text-cleaning step. Keep case when capitalization carries meaning, such as in some named entities, product identifiers, passwords, or programming-language syntax.
Exercise
Which method should you normally use to compare "Straße" and "STRASSE" without treating case as meaningful, and why?
Show answer
Use casefold() on both strings. It applies Unicode-aware caseless matching and maps "ß" to "ss", so both comparison keys become "strasse".