Python casefold() String Method

Learn Python string casefold() with syntax, examples, Unicode case-insensitive comparison, casefold vs lower, and common mistakes when comparing strings.

Published

Updated

Read time 4 min read

Reviewed byDeepak Prasad

Python casefold() String Method

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

text
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

python
text = "HELLO"
print(text.casefold())
Output

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.

python
a = "User-Name"
b = "user-name"
print(a.casefold() == b.casefold())
Output

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.

python
text = "Hello"
print(text.lower())
print(text.casefold())
Output

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()

python
text = "straße"

print(text.lower())     # straße
print(text.casefold())  # strasse
Output

Here 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.

python
items = ["GOLINUXCLOUD", "Is", "Best"]
normalized = [s.casefold() for s in items]
print(normalized)
Output

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 when casefold() would align with folding rules (for example ß / ss behavior).
  • Assuming casefold() mutates the original string; it always returns a new str.
  • Using casefold() output for user-visible “make this pretty lowercase” text; folding can change characters in ways that look odd on screen—prefer lower() 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 needs unicodedata.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


Frequently Asked Questions

1. What is the difference between casefold() and lower() in Python?

lower() applies simple Unicode lowercasing suited to readable text; casefold() applies Unicode case folding, which is more aggressive and intended for case-insensitive comparisons—for example German ß becomes ss under casefold() but not under lower().

2. When should str.casefold() be used?

Use casefold() when normalizing both sides of a case-insensitive string comparison or lookup, especially with international text; use lower() for user-visible lowercasing where case folding would look wrong.

3. Does casefold() modify the original string?

No—Python strings are immutable; casefold() returns a new str instance and leaves the original unchanged.
Deepak Prasad

R&D Engineer

Founder of GoLinuxCloud with more than 15 years of expertise in Linux, Python, Go, Laravel, DevOps, Kubernetes, Git, Shell scripting, OpenShift, AWS, Networking, and Security. With extensive …