Python Reference Library →

Python String casefold() – Case-Insensitive String Comparison

Let’s look at string casefold() – case-insensitive string comparison

  1. Notice

    casefold() applies aggressive Unicode normalization of case for caseless matching. It can change characters that lower() leaves different.

  2. Try

    Follow the example, change its sample, and compare what happens.

  3. Check

    Explain the result and test your understanding.

Watch how it works

Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.

Explore the idea

Follow the code and watch its state change. Guided simulation.

Code
Python
text = 'Straße'
result = text.casefold()
print(result)
Live state

Top-level scope

text→Object 1
result→Object 2
Object 1 · str'Straße'Object 2 · str'strasse'
This step

casefold() applies aggressive Unicode normalization of case for caseless matching. It can change characters that lower() leaves different.

Picture string casefold() – case-insensitive string comparison

casefold() applies aggressive Unicode normalization of case for caseless matching. It can change characters that lower() leaves different.

Image brief

Alt text
A diagram for Python String casefold() – Case-Insensitive String Comparison. Instructions: text = 'Straße' result = text.casefold() print(result). Displayed result: strasse. The final example bindings are text --> object 1 (str); result --> object 2 (str). object 1: 'Straße'; object 2: 'strasse'
Image generation prompt
Create a clean educational illustration for a Python programming lesson titled "Python String casefold() – Case-Insensitive String Comparison". Teaching focus: casefold() applies aggressive Unicode normalization of case for caseless matching. It can change characters that lower() leaves different. Depict exactly the relationships and labels in this reference diagram: EXAMPLE INSTRUCTIONS text = 'Straße' result = text.casefold() print(result) | v DISPLAYED RESULT strasse FINAL BINDINGS text --> object 1 (str) result --> object 2 (str) object 1: 'Straße' object 2: 'strasse' Use a flat vector style, generous spacing, clear arrows and legible labels, with a restrained blue/teal palette on a plain background. The diagram must explain the programming idea accurately. Do not depict variables as boxes containing copied objects when they share a reference. Avoid mascots, decorative screenshots, logos, gradients and extra text. Landscape composition, approximately 3:2, suitable for a course card. Accessible description: A diagram for Python String casefold() – Case-Insensitive String Comparison. Instructions: text = 'Straße' result = text.casefold() print(result). Displayed result: strasse. The final example bindings are text --> object 1 (str); result --> object 2 (str). object 1: 'Straße'; object 2: 'strasse'

Follow the execution

Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.

Execution workspace

Choose a sample and follow its execution. Guided simulation.

Code
Python
text = 'Straße'
result = text.casefold()
print(result)
Live state

Top-level scope

text→Object 1
result→Object 2
Object 1 · str'Straße'Object 2 · str'strasse'
This step

The example has finished. Compare the final output with the values and references above.

Terminal
Output
strasse

What do you expect this program to print?

What does this example display?

text = 'Straße'
result = text.casefold()
print(result)
What does this example display?
Choose one answer. You can try again.

Answer and explanation

strasse

Follow the printed values in order. The output is: strasse

Which explanation matches what you learned?

Which explanation matches what you learned?
Choose one answer. You can try again.

Answer and explanation

casefold() applies aggressive Unicode normalization of case for caseless matching. It can change characters that lower() leaves different.

casefold() applies aggressive Unicode normalization of case for caseless matching. It can change characters that lower() leaves different.

More examples and reference

Python String casefold() Method

The casefold() method in Python is used to convert a string into a case-insensitive comparable form. It is more aggressive than lower() and is recommended for comparisons that ignore case, especially in international text.

Syntax

string.casefold()

Parameters:

  • None – casefold() does not take any parameters.

Returns:

  • A new string where all characters are converted to lowercase in a casefolded form (ready for case-insensitive comparison).

Example: Comparing Two Strings

str1 = "Straße"
str2 = "strasse"

print(str1.casefold() == str2.casefold())
True

Why? Because "ß" is casefolded to "ss" – something lower() doesn’t do.

Example: Basic Usage

text = "HELLO World"
print(text.casefold())
hello world

Use Case: Case-Insensitive Search

word = "Python"
user_input = "pYThOn"

if word.casefold() == user_input.casefold():
    print("Match found!")
Match found!

casefold() vs lower()

Feature lower() casefold()
Converts to lowercase Yes Yes
Suitable for international comparison No Yes
More aggressive Unicode conversion No Yes

Common Mistakes

  • Mistake: Trying to use casefold() on non-string data.
  • Solution: Always ensure the variable is a string: str(your_variable).casefold()

Interview Tip

Use casefold() when asked to perform case-insensitive string comparison in a robust and Unicode-safe way.

Summary

  • casefold() is used for case-insensitive string comparison.
  • It handles special characters better than lower().
  • Always returns a new, casefolded string.

Practice Problem

Write a function that checks if two user-entered strings are equal, ignoring case and accents.

def is_equal_case_insensitive(str1, str2):
    return str1.casefold() == str2.casefold()

# Try it!
print(is_equal_case_insensitive("Straße", "STRASSE"))

Expected Output:

True

What to remember

Connect what you noticed in the example with the idea below.

Remember

casefold() applies aggressive Unicode normalization of case for caseless matching. It can change characters that lower() leaves different.

Watch out for this idea

casefold() is always identical to lower().

This is the misconception to avoid.

Reference checks are optional and do not count toward course progress.

    Reference library →