Python casefold() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

In Python, casefold() returns a case-folded copy of a string for case-insensitive matching. It is stronger than lower() for Unicode text.

It takes no arguments. Fold both sides before you compare, and keep the original text for display.

Syntax

Call casefold on the string. Start with this line.

python
folded = text.casefold()
CallWhat it does
"Hello".casefold()Returns a folded form for matching.
a.casefold() == b.casefold()Case-insensitive equality check.

Prefer casefold() over lower() when international text may appear.

Library declarations
python
str.casefold() → str

Parameters

This method has no parameters.

Return Value

Return ValueDescription
strA new case-folded string for comparisons.

Python Examples

Example 1 — Basic casefold()

Fold uppercase text and confirm the original string stays unchanged.

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

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

How It Works

  1. Start uppercase. original is "HELLO".
  2. Call casefold. Returns a new folded string into folded.
  3. ASCII looks like lower. Result is "hello".
  4. Source untouched. Immutability keeps original as "HELLO".

Example 2 — Case-Insensitive Comparison

Accept any casing of the same word by folding before ==.

python
answer = "PyThOn"

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

How It Works

  1. Mixed input. User typed "PyThOn".
  2. Fold the answer. Becomes "python".
  3. Compare to a folded target. Literal "python" is already folded.
  4. Match succeeds. Letter case no longer blocks equality.

Example 3 — casefold() vs lower()

The classic Unicode win: German ß folds to ss.

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

  1. lower() keeps ß. "Straße".lower() → "straße", not equal to "strasse".
  2. casefold() expands ß. Maps eszett to ss → "strasse".
  3. STRASSE folds too. Becomes "strasse".
  4. Equality holds. Prefer casefold() for real-world matching.

Frequently Asked Questions

It returns a case-folded copy of a string for case-insensitive matching.
For ASCII they often match. casefold() applies stronger Unicode rules, such as German ß → ss.
Use it for case-insensitive compares, search, usernames, and international text.
No. It returns a new string.
Python 3.3 and later.

Next: center()

Center text within a fixed width using padding characters.

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.

9 people found this page helpful