Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

Python String Casefold: Unicode-Safe Case-Insensitive Comparison

Python's casefold() method provides Unicode-safe case-insensitive string comparison, unlike lower(), with practical examples and edge cases.

~4 min readPython
#casefold#unicode#string comparison#case-insensitive
Illustration of Python string casefold normalizing Unicode characters for case-insensitive comparison.

Python's str.casefold() returns a copy of a string with all case distinctions removed. It is designed for Unicode-aware, case-insensitive comparisons, and it can map characters such as ß to ss where lower() would leave them unchanged.

Why lower() Is Not Enough for Case-Insensitive Comparisons

When you need to compare strings without regard to case, the first instinct is often to call lower() on both sides. For ASCII text, that works. But Python strings can contain Unicode characters, and some of them do not have a straightforward lowercase mapping. A well-known example is the German character ß (sharp s). lower() returns "ß" unchanged, so "ß".lower() == "ss".lower() evaluates to False, even though a case-insensitive comparison should treat them as equivalent.

That is why Python provides casefold(). The casefold() method was introduced in Python 3.3 to provide a more aggressive form of case folding that is intended for caseless matching. It goes beyond simple lowercase conversion and applies Unicode case folding rules, which handle mappings like ß to ss.

What casefold() Does Differently

casefold() is similar to lower() but applies a more complete set of Unicode transformations. It is designed specifically for case-insensitive comparisons. The method returns a string that is suitable for caseless matching. For most characters, casefold() and lower() produce the same result, but for certain Unicode characters, casefold() performs additional mappings. For example:

python
print("ß".casefold()) # 'ss' print("ß".lower()) # 'ß'

The difference comes from the Unicode standard's case folding rules, which define how characters should be treated when case should be ignored. casefold() uses the full case folding mapping, while lower() uses the default lowercase mapping.

Practical Examples: Using casefold() for Normalization

A common use case is normalizing user input before storing or comparing it. For example, if you are building a login system and want usernames to be case-insensitive, you might store the normalized version. Using casefold() ensures that Unicode characters like ß are handled correctly.

python
def normalize_username(username: str) -> str: return username.casefold() username1 = "Straße" username2 = "STRASSE" print(normalize_username(username1) == normalize_username(username2)) # True

Without casefold(), this comparison would fail if you used lower() instead. This is especially important in international applications where users may have names with non-ASCII characters.

Comparing Strings with casefold() vs lower()

The choice between casefold() and lower() depends on whether you need Unicode-aware caseless matching. For ASCII-only data, both work identically. For any text that may contain Unicode, casefold() is the safer choice for comparisons.

Operationlower()casefold()
ASCII lettersConverts to lowercaseConverts to lowercase
ß (sharp s)Leaves it as ßMaps to ss
ς (Greek final sigma)Leaves it as ςMaps to σ
ẞ (capital sharp s)Maps to ßMaps to ss
Intended useDisplay and general text processingCaseless matching and normalization for comparison

The table shows that casefold() handles special cases that lower() does not. If you are comparing strings for equality, searching, or grouping by case-insensitive keys, casefold() is a better default.

Performance and Runtime Considerations

Because casefold() applies additional Unicode mappings, it can be slightly more expensive than lower() in some implementations. For typical string lengths, the difference is usually negligible. If you are performing millions of comparisons in a hot loop, you can pre-normalize strings once and then compare the normalized versions, rather than calling casefold() on every comparison.

python
# Pre-normalize once normalized = [s.casefold() for s in strings]

This avoids repeated processing and is a common optimization pattern. The actual performance cost depends on the Python implementation and the specific Unicode characters involved, but in practice it is rarely a bottleneck.

Edge Cases and Unicode Behavior

casefold() is not a perfect solution for every language. Some languages have context-sensitive case rules that cannot be resolved by a single character mapping. Greek sigma is a useful example: the final form ς and the normal form σ both fold to σ with casefold(). But other linguistic rules may require more than character-by-character folding. For instance, German ß is often treated as equivalent to ss, but the reverse is not always true in every context. Unicode case folding is a good general-purpose approach, but it is not the same as a full language-specific collation order.

Another edge case is handling characters that are already lowercase. casefold() is idempotent: calling it twice returns the same result. This is useful when you cannot guarantee the input is already normalized.

python
s = "Straße" print(s.casefold() == s.casefold().casefold()) # True

When to Use casefold() and When to Avoid It

Use casefold() when you need case-insensitive comparison, searching, or sorting of strings that may contain Unicode characters. This includes user-generated content, internationalized applications, and any scenario where you cannot assume ASCII-only input.

Avoid casefold() when you need to preserve the original case for display purposes. casefold() is a case folding transformation, not a formatting tool. It should not be used to lowercase strings for presentation. For that, lower() is more appropriate. Also, if you are working with a language-specific collation that has rules beyond the Unicode standard, you may need a more specialized library like pyuca or the locale module.

In summary, str.casefold() is the right tool for Unicode-aware caseless matching. It handles edge cases that lower() misses and is the recommended default for case-insensitive comparisons.

Try matching browser tools

All tools

Small utilities that fit Python work — they run entirely in this browser tab, so nothing you paste leaves your device.

All Python guides

More in Python

All Python guides

The guides filed next to this one in Python, in library order.