str.casefold()

str.casefold()
Returns: str · Updated March 13, 2026 · String Methods
strings case unicode comparison

The casefold() method returns a casefolded copy of a string. Casefolding is more aggressive than lowercasing—it converts all characters to their casefolded form, which makes it ideal for case-insensitive string matching, especially when dealing with Unicode characters.

Unlike lower(), casefold() handles Unicode characters that have special case conversion rules. For example, the German letter “ß” (eszett) becomes “SS” when casefolded, whereas lower() leaves it unchanged.

Syntax

str.casefold()

Parameters

This method takes no parameters.

Examples

Basic usage

text = "Hello World"
folded = text.casefold()
print(folded)
# hello world

Unicode handling

# German eszett transforms to 'ss'
german = "ß"
print(german.casefold())
# ss

# Greek sigma has different lowercase forms
greek = "Σ"
print(greek.lower())
# σ
print(greek.casefold())
# σ

Case-insensitive comparison

# Without casefold - fails for special characters
print("ß".lower() == "SS".lower())
# False

# With casefold - works correctly
print("ß".casefold() == "SS".casefold())
# True

Common Patterns

Normalizing user input

def normalize_username(username):
    """Normalize username for case-insensitive lookup."""
    return username.casefold()

# All of these map to the same identity
users = ["Alice", "ALICE", "alice", "alicE"]
normalized = [normalize_username(u) for u in users]
print(set(normalized))
# {'alice'}

Case-insensitive dictionary keys

data = {"ALPHA": 1, "BETA": 2}

key = "alpha"
print(data.get(key))
# None

# Casefolded lookup
key_folded = key.casefold()
print(data.get(key_folded.upper()))
# 1

Sorting strings ignoring case

words = ["Apple", "banana", "CHERRY", "date"]

# Casefolded sort
sorted_words = sorted(words, key=lambda w: w.casefold())
print(sorted_words)
# ['Apple', 'banana', 'CHERRY', 'date']

See Also