Skip to content
Tutorials Center
My learning

Topics

Python Reference · String Methods

Python str.casefold() method

Reference for Python str.casefold(): aggressive lower-casing for caseless comparison, how it differs from lower(), with German and Greek examples.

The str.casefold() method returns a case-folded copy of a string. Case folding is like lower-casing, but more aggressive: it is designed so that two strings that differ only in letter case compare equal, even in languages where lower() is not enough.

Use it for caseless comparisons, such as matching user input against a list of names. Do not use it to display text to people, because the result can look unusual (the German "ß" becomes "ss").

Python
print("Straße".casefold() == "STRASSE".casefold())   # True

Try it

Python casefold() versus lower()

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Python A caseless lookup

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Syntax

Python
str.casefold()
Name Description
(none) The method takes no arguments.

Return value: a new str, possibly longer than the original.

Raises: nothing for a string.

Usage notes

  • For comparison only. The folded text is a comparison key, not a pretty form. Keep the original string for display and fold a copy for matching.
  • Can change the length. "ß" folds to "ss", so a folded string may have more characters than the original.
  • Beats lower() for non-English text. Greek final sigma ("ς") and German sharp s are handled, so equivalent words compare equal. For plain English words, lower() gives the same result.
  • Fold both sides. Comparing a.casefold() == b is a bug unless b is already folded.
  • Not a full Unicode normalization. The same visible text can still be written with different code point sequences (a letter plus a combining accent, or one precomposed character). For robust comparison, use unicodedata.normalize("NFC", s) first, then casefold().
  • Original is unchanged. Strings are immutable.
  • Regular expressions have their own flag for the same purpose: re.IGNORECASE.