Python casefold() Method

Beginner
⏱️ 8 min read
📚 Updated: Jul 2026
🎯 5 Examples
String Methods

What You’ll Learn

The casefold() method returns a case-folded version of a string, optimized for case-insensitive comparisons. Added in Python 3.3, it looks similar to lower(), but uses stronger Unicode rules—making it the right choice when you need reliable matching across different letter cases and international text.

01

Case Folding

Normalize text for matching.

02

Syntax

One method, no arguments.

03

vs lower()

Know when each is better.

04

Immutable

Original string stays unchanged.

05

Search & Match

Compare user input safely.

06

Unicode Ready

Handles non-ASCII letters.

Definition and Usage

In Python, casefold() is a string method that returns a copy of the string with case distinctions removed as aggressively as possible. Case folding is a Unicode process used when you want two strings to compare equal regardless of uppercase or lowercase differences. For example, comparing "PYTHON" and "python" becomes straightforward: both become "python" after casefold().

💡
Beginner Tip

When checking if two strings are the same ignoring case, prefer s1.casefold() == s2.casefold() over comparing raw strings or only using lower(), especially if your data may include international characters.

📝 Syntax

The casefold() method takes no parameters:

python
string.casefold()

Syntax Rules

  • string — any valid Python str object.
  • Return value — a new case-folded str; the original is never modified.
  • No arguments — passing arguments raises TypeError.
  • Empty string — returns "" without error.
  • Python 3.3+ — available in modern Python 3; not in Python 2.

⚡ Quick Reference

Inputcasefold() Result
"HELLO""hello"
"Python""python"
"Straße""strasse"
""""
"123-ABC""123-abc"
Basic
"HELLO".casefold()

Returns "hello"

Compare
a.casefold() == b.casefold()

Case-insensitive equality

Search
needle.casefold() in hay.casefold()

Case-insensitive substring

Normalize
key = user_input.casefold()

Dictionary lookup key

Examples Gallery

Run these examples in Python 3.3 or later. Each one demonstrates a practical pattern for case-insensitive string handling.

📚 Getting Started

See how casefold() converts uppercase text to a normalized lowercase form.

Example 1 — Basic casefold()

The simplest use: fold an uppercase string before storing or comparing it.

python
original = "HELLO"
folded = original.casefold()

print("Original:", original)
print("Casefolded:", folded)

How It Works

  • original remains "HELLO" because strings are immutable.
  • casefold() returns a new string with all cased letters folded to lowercase.
  • For plain ASCII uppercase, the result looks the same as lower().

Example 2 — Case-Insensitive Comparison

Accept user input regardless of how they type capital letters.

python
answer = "PyThOn"

if answer.casefold() == "python":
    print("Correct! Case did not matter.")
else:
    print("Try again.")

How It Works

Both sides are case-folded before comparison, so "PyThOn" matches "python", "PYTHON", or any other casing variant.

📈 Practical Patterns

Patterns you will use in real programs for search, matching, and normalization.

Example 4 — casefold() vs lower()

This is the key reason casefold() exists: stronger Unicode handling than lower().

python
word_a = "Straße"
word_b = "STRASSE"

print("lower():   ", word_a.lower() == word_b.lower())
print("casefold():", word_a.casefold() == word_b.casefold())

How It Works

  • "Straße".lower() becomes "straße", which is not equal to "strasse".
  • casefold() maps the German eszett (ß) to ss, so both words match.
  • For ASCII-only English text, lower() and casefold() usually agree—but casefold() is safer for comparisons.

Example 5 — Normalizing Dictionary Keys

Use case-folded keys so lookups work no matter how the user types.

python
users = {
    "alice": "Alice Smith",
    "bob": "Bob Jones"
}

def lookup(name):
    return users.get(name.casefold(), "User not found")

print(lookup("ALICE"))
print(lookup("BoB"))

How It Works

Dictionary keys are stored in lowercase. Each lookup folds the input first, so "ALICE" and "BoB" still find the correct entries.

🚀 Common Use Cases

  • Password or keyword checks — compare answers without rejecting valid casing variants.
  • Search filters — find substrings in titles, tags, or filenames case-insensitively.
  • Username lookups — treat "Admin" and "admin" as the same account key.
  • Data deduplication — normalize text before checking for duplicates.
  • International text — compare Unicode strings more reliably than plain lower().

🧠 How casefold() Works

1

Python reads the string

You call casefold() on a str object—user input, file content, or a literal.

Input
2

Unicode case folding runs

Python applies case-folding rules that remove case distinctions as aggressively as possible, including special Unicode cases.

Transform
3

A new string is returned

The folded string is ready for equality checks, substring searches, or use as a normalized key.

Output
=

Reliable matching

Compare, search, and store text without being tripped up by letter case.

📝 Notes

  • casefold() is for comparison and normalization, not for pretty display—use capitalize() or title() when formatting text for users.
  • For most English-only ASCII strings, lower() and casefold() return the same result, but casefold() is still the better choice for equality checks.
  • It does not remove accents or whitespace—only case distinctions are folded.
  • Always case-fold both sides of a comparison for consistent results.

Conclusion

The casefold() method is Python’s go-to tool for case-insensitive string work. It goes beyond simple lowercasing by applying Unicode-aware folding rules, which makes your comparisons more reliable when users type in different cases or when text includes international characters.

Reach for casefold() whenever equality or search should ignore case. Keep lower() for display formatting, and use capitalize() or title() when you want text to look polished on screen.

💡 Best Practices

✅ Do

  • Use casefold() for case-insensitive equality checks
  • Fold both strings before comparing them
  • Prefer it over lower() for international text
  • Normalize user input before dictionary lookups
  • Combine with strip() to trim whitespace first

❌ Don’t

  • Use casefold() when you need display formatting
  • Assume it removes accents or punctuation
  • Compare only one side after folding
  • Pass arguments to casefold()
  • Expect it to replace locale-specific sorting rules

Key Takeaways

Knowledge Unlocked

Five things to remember about casefold()

Use these points when writing case-insensitive Python code.

5
Core concepts
🔄 02

Returns New str

Original stays the same.

Immutable
🌐 03

Unicode Aware

Stronger than lower().

Unicode
🔐 04

Compare Safely

Fold both sides first.

Pattern
05

Not for Display

Use capitalize/title instead.

Edge case

❓ Frequently Asked Questions

casefold() returns a case-folded copy of a string for case-insensitive matching. It is similar to lower(), but applies more aggressive Unicode rules so strings that should match in a case-insensitive way compare equal after casefold().
Call it on any string: string.casefold(). It takes no arguments and returns a new str. The original string is not modified.
For plain ASCII text they usually return the same result. casefold() is preferred for case-insensitive comparisons because it handles special Unicode cases more aggressively—for example, German ß becomes ss with casefold().
Use casefold() when comparing strings without caring about letter case, especially with international text, usernames, search queries, or dictionary keys.
No. Python strings are immutable. casefold() always returns a new string.
casefold() was added in Python 3.3. It is not available in Python 2.

Explore More Python String Methods

Continue with center(), count(), and the rest of the string method reference.

Next: center() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

7 people found this page helpful