The Python casefold function converts all the characters in a given string into Lowercase letters. Though this is the same as the lower function, it is aggressive and stronger than the lower.
This method is very useful when we are comparing strings. The syntax of this Python string casefold function is
String_Value.casefold()
Python casefold Example
The following set of examples helps you understand this method.
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
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 this 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')