Python String casefold()

Summary: in this tutorial, you’ll learn how to use the Python string casefold() method to carry a case-insensitive string comparison.

Introduction to the Python string casefold() method

The Python string casefold() method returns a casefolded copy of the string.

Casefolding is like case lowering. However, casefolding is more aggressive because it’s intended to remove all case distinctions in a string.

If you use purely ASCII text, the lower() and casefold() methods return the same result.

However, if you deal with Unicode characters, the casefold() method returns a more accurate result than the lower() method.

For example, the letter 'ß' in Germany is equivalent to 'ss'. If you use the lower() method, the result would be 'ß' because the letter 'ß' is already lowercase.

However, if you call the casefold() method on the string 'ß', it would return the 'ss' instead.

For this reason, you should use the string casefold() method to carry case-insensitive string comparisons to achieve a more accurate result.

Python string casefold() method example

The following example illustrates how to use the lower() and casefold() methods to compare string case-insensitively:

color1 = 'weiß'
color2 = 'weiss'

print(color1 == color2)  # False
print(color1.lower() == color2.lower())  # False
print(color1.casefold() == color2.casefold())  # TrueCode language: PHP (php)

Output:

False
False
TrueCode language: PHP (php)

How it works.

The following expression returns False because the lower() method doesn’t do anything to the letter 'ß':

color1.lower() == color2.lower()

However, the following comparison returns True because the casefold() method returns a copy of the color2 string with the letter 'ß' converted to 'ss'

Summary

  • Use string lower() method to carry case-insensitive string comparisons for strings with ASCII characters.
  • Use string casefold() method to perform case-insensitive comparisons for strings with Unicode characters.
Did you find this tutorial helpful ?