Python casefold

The Python casefold function converts all the characters in a given string into lowercase letters. Although casefold() is the same as the lower function, it is more aggressive and stronger than the lower. As the casefold() function supports Unicode characters and non-ASCII letters, it is useful when comparing strings.

casefold() syntax

The syntax of this string casefold function is

String_Value.casefold()

Parameters: It does not accept any arguments.

Return Value: It converts all characters in the original string into lowercase letters. The casefold() function converts text into a case-insensitive form suitable for Unicode comparison and returns a new string.

Python casefold Example

The following set of examples helps you understand the casefold() method. Here, we apply the casefold() function to convert the mixed characters string to lowercase and also use numeric values.

Str1 = 'leaRn PYTHON PrOGRAms AT tutOrial gateWay'
 
Str2 = Str1.casefold()
print('First Output after is = ', Str2)
 
# Observe the Difference between Original and other
print('Original is = ', Str1)
print('Second Output is = ', Str1.casefold())
 
# Use directly
Str3 = 'LearN pYTHON proGramminG'.casefold()
print('Third Output after is = ', Str3)
 
Str4 = 'pyTHon ProGRAmming 1234 tuTorIal'.casefold()
print('Fourth Output after is = ', Str4)
First Output after is =  learn python programs at tutorial gateway
Original is =  leaRn PYTHON PrOGRAms AT tutOrial gateWay
Second Output is =  learn python programs at tutorial gateway
Third Output after is =  learn python programming
Fourth Output after is =  python programming 1234 tutorial

Use Python casefold for case-insensitive string comparison

This casefold function is very useful in string comparison. The Python example below compares strings with different case characters of the same word. First, we compared without using the casefold() method. Next, we used this function inside the If Statement. Please refer to the lower method article.

Str1 = 'PYthoN'
Str2 = 'pyTHon'
 
if(Str1 == Str2):
    print('Both are equal')
else:
    print('Not Equal')
           
if(Str1.casefold() == Str2.casefold()):
    print('Both are equal')
else:
    print('Not Equal')
Not Equal
Both are equal

This casefold example is the same as the above. However, this time we are allowing the user to enter their own sentences. Next, we compare those texts.

str1 = input("Please enter the First String : ")
str2 = input("Please enter the Second String : ")
 
if(str1.casefold() == str2.casefold()):
    print('Both are equal')
else:
    print('Not Equal')
Python casefold function Example

Use casefold on the list of strings

In the following example,w e declare a list of strings with lower and uppercase characters. The list comprehension with a for loop iterates over the list of strings, and the casefold () converts them to lowercase. This example is handy to normalize the CSV data, or any dataset.

text = ["USA", "UK", "India", "Canada"]
t = [c.casefold() for c in text]
print(t)
['usa', 'uk', 'india', 'canada']

Difference between Python casefold() and lower()

Both the casefold() and lower() functions convert the uppercase characters in a given string to lowercase. However, the casefold() method is more aggressive and supports the non-ASCII (Unicode) characters.

In the following example, we use the German letter (non-ASCII character). Here, the lower() function converts the first character to lowercase and leaves the German letter unchanged. However, the casefold() function converts it to lowercase.

text = 'LOß'
print(text.lower())
print(text.casefold())
loß
loss