The str.casefold() method returns a normalized form of a string for case-insensitive matching. It is related to lower() but follows Unicode case folding rules, which are stronger than plain lowercasing. That matters as soon as text leaves ASCII—for example German ß stays straße with lower() but becomes strasse with casefold(), so two spellings can compare equal when both sides are casefolded. The official docs describe casefold() as more aggressive than lower() for that reason.
Tested on: Python 3.13.3; kernel 6.14.0-37-generic.
What is casefold() in Python?
casefold() is a string method on str. It takes no arguments and returns a new string suitable for case-insensitive comparison: every character is mapped through the Unicode case-folding table, not only “make letters small” rules. It is a good normalization for operations such as stored usernames, slugs, filenames in a tolerant search, or dictionary keys compared without regard to case—whenever two strings should match under the same folding rules, not only when ASCII upper/lower would agree.
Python casefold() syntax
str.casefold()There are no parameters. The return value is always a str (a casefolded copy of the original; strings are immutable, so the caller always gets a new object for comparisons).
Basic casefold() example
text = "HELLO"
print(text.casefold())That prints hello. The original text is unchanged because strings are immutable.
Use casefold() for case-insensitive comparison
For equality checks, apply casefold() to both operands so neither side keeps mixed casing or locale-specific surprises.
a = "User-Name"
b = "user-name"
print(a.casefold() == b.casefold())That prints True. The same pattern scales to user input versus stored values: normalize once with casefold() before comparing or before inserting into a case-insensitive index. For broader equality checks, see Python compare strings.
casefold() vs lower() in Python
lower() is appropriate when the goal is readable lowercase for display or simple Latin text. casefold() is appropriate when the goal is stable case-insensitive matching under Unicode rules. casefold() is not documented as faster than lower(); the difference is semantic strength, not speed.
text = "Hello"
print(text.lower())
print(text.casefold())For plain ASCII like Hello, lower() and casefold() usually match. The divergence appears with special cases such as sharp s (below).
Unicode example: why casefold() is stronger than lower()
text = "straße"
print(text.lower()) # straße
print(text.casefold()) # strasseHere lower() keeps a single ß in the output, while casefold() expands the compatibility mapping to ss, which is how case-insensitive matching is expected to treat that character in Unicode. That is the main practical reason to prefer casefold() when comparing identifiers or natural-language strings across locales.
Use casefold() with lists of strings
Apply casefold() per element when normalizing a collection for lookup or sorting keys. A list comprehension keeps the intent clear; tuples and other iterables use the same pattern.
items = ["GOLINUXCLOUD", "Is", "Best"]
normalized = [s.casefold() for s in items]
print(normalized)That prints ['golinuxcloud', 'is', 'best']—each token normalized the same way a single string would be. For case checks on individual tokens, compare with isupper() or related string methods after normalization.
Common mistakes with casefold()
- Using
lower()alone for Unicode-aware case-insensitive comparison whencasefold()would align with folding rules (for exampleß/ssbehavior). - Assuming
casefold()mutates the original string; it always returns a newstr. - Using
casefold()output for user-visible “make this pretty lowercase” text; folding can change characters in ways that look odd on screen—preferlower()or locale-aware APIs for display. - Comparing only one side with
casefold()while the other stays mixed case; both sides should be normalized the same way. See check if a string contains a substring for membership checks that also need case folding. - Treating
casefold()as Unicode normalization (NFC/NFKD, accent stripping, compatibility decomposition); it only performs case folding. Full normalization still needsunicodedata.normalize()or similar when accents and composed characters matter beyond case.
Python casefold() quick reference table
| Need | Use |
|---|---|
| Convert string for basic lowercase display | lower() |
| Normalize for case-insensitive matching | casefold() |
| Compare two strings case-insensitively | a.casefold() == b.casefold() |
| Process a list of strings | [s.casefold() for s in items] |
| Original string changed? | No—strings are immutable |
Summary
str.casefold() exists so programs can compare strings in a Unicode-correct, case-insensitive way. It is stricter than lower(): same idea for ASCII, different—and more useful—behavior for characters such as German ß. Use casefold() when folding both sides of a comparison or building a case-insensitive key; use lower() for ordinary lowercasing and display. Remember that case folding is not full normalization, so accent and compatibility issues still need explicit handling when the product requires them. For structured sequence types in general, see Python tuple.
References
- str.casefold (Python standard library)
- str.lower (Python standard library)

