Returns a casefolded string
Usage
The casefold()
method returns a casefolded (lowercase but more aggressive) copy of the string. This method does not change the original string.
Casefolded strings are usually used to ‘normalize‘ text for the purposes of caseless comparison (especially when you want to take characters of many different languages into account).
Syntax
string.casefold()
Basic Example
# Make a string casefolded
S = 'Hello, World!'
x = S.casefold()
print(x)
# Prints hello, world!
casefold() vs lower()
Casefolding is similar to lowercasing but more aggressive because it is intended to remove all case distinctions in a string.
For example, the German lowercase letter ‘ß‘ is equivalent to ‘ss‘. Since it is already lowercase, lower() would do nothing to ‘ß‘, but casefold()
converts it to ‘ss‘.
S = 'Das straße'
x = S.casefold()
print(x)
# Prints das strasse
S = 'Das straße'
x = S.lower()
print(x)
# Prints das straße
If you are working strictly in the English language, lower()
and casefold()
returns exactly the same results.
However, if you are trying to normalize text from other languages that use more than English 26-letter alphabet, use casefold()
to compare your strings for more consistent results.