Python String casefold() Method

The casefold() method returns a string where all the characters are in lower case. It is similar to the lower() method, but the casefold() method converts more characters into lower case.

For example, the German lowercase letter 'ß' is equivalent to 'ss'. Since it is already lowercase, the lower() method would not convert it; whereas the casefold() converts it to 'ss'.

Use the casefold() method if your application supports globalization or localization.

Syntax:

str.casefold()

Parameters:

No parameters.

Return Value:

Returns the lower case string.

The following example demonstrates the casefold() method.

Example:
mystr = 'TUTORIALS Teacher'
print(mystr.casefold())

print('HELLO WORLD'.casefold())
Output
tutorials teacher
hello world

Symbols and Letters are not affected by the casefold() method. If the string is in lower cases then casefold() method will return original string.

Example:
mystr = '1# HarBOR Side'
print(mystr.casefold())

mystr = 'Python is a programming language'
print(mystr.casefold())
Output
1# harbor side
python is a programming language

The following example shows the difference between the lower() and casefold() methods.

Example:
mystr = 'außen'
print(mystr.casefold())

mystr = 'außen'
print(mystr.lower())
Output
aussen
außen

In the above example, since ß is already lowercase, the lower() method does nothing to it. But, casefold() converts it to ss.

Want to check how much you know Python?