Two names may have different casing while representing the same identifier.
What You Need to Understand
lower() applies lowercase conversion, while casefold() performs a more aggressive Unicode normalization intended for case-insensitive comparisons. Neither makes text equivalent across all languages or normalization forms.
When to Use It
Two names may have different casing while representing the same identifier.
Step-by-Step Python Example
first_name = "Straße"
second_name = "STRASSE"
print(first_name.lower() == second_name.lower())
print(first_name.casefold() == second_name.casefold())
How the Example Works
lower() leaves the German sharp-s character unchanged, so the first comparison is false. casefold() expands it, making the second comparison true.
Example Output
False
True
{'python'}
A Second Example
Compare this smaller variation with the main example, then experiment with your own values.
labels = ["PYTHON", "Python", "python"]
normalized = {label.casefold() for label in labels}
print(normalized)
Important Behavior and Edge Cases
Case folding is for comparisons, not for presentation. For international inputs you may also need unicodedata.normalize() and a carefully defined identity policy.
Try It Yourself
Compare "Μάιος" with different casing and observe that Unicode rules can be language-sensitive.
Common Beginner Mistakes
- Case folding is for comparisons, not for presentation. For international inputs you may also need
unicodedata.normalize()and a carefully defined identity policy. - Applying a string method before defining whether text is intended for display, comparison, parsing, or input validation.
Key Takeaways
lower()applies lowercase conversion, whilecasefold()performs a more aggressive Unicode normalization intended for case-insensitive comparisons.lower()leaves the German sharp-s character unchanged, so the first comparison is false.- Case folding is for comparisons, not for presentation.