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.
Fundamentals
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.
Foundation
📝 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.
Cheat Sheet
⚡ Quick Reference
Input
casefold() 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
Hands-On
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)
📤 Output:
Original: HELLO
Casefolded: hello
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.")
📤 Output:
Correct! Case did not matter.
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 3 — Case-Insensitive String Matching
Search for a keyword inside a list of strings without worrying about letter case.
python
titles = [
"Case-Insensitive Search",
"Python String Methods",
"Working with Unicode"
]
query = "UNICODE"
for title in titles:
if query.casefold() in title.casefold():
print("Match found:", title)
📤 Output:
Match found: Working with Unicode
How It Works
Both the search query and each title are case-folded before the in check, so "UNICODE" matches "Unicode" inside the third title.
Example 4 — casefold() vs lower()
This is the key reason casefold() exists: stronger Unicode handling than lower().
Dictionary keys are stored in lowercase. Each lookup folds the input first, so "ALICE" and "BoB" still find the correct entries.
Applications
🚀 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.
Important
📝 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.
Wrap Up
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.
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
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about casefold()
Use these points when writing case-insensitive Python code.
5
Core concepts
🔍01
Case-Insensitive
Built for matching text.
Purpose
🔄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.