The Python casefold String function converts all the characters in a given string into Lowercase letters. Though this is the same as the string lower function, casefold is aggressive and stronger than lower.
This string casefold function is very useful when we are comparing the strings. The syntax of this string casefold function in Python Programming Language is
Python casefold Example
The following set of examples help you understand the string casefold function.
# Python casefold Method Example Str1 = 'leaRn PYTHON PrOGRAms AT tutOrial gateWay' Str2 = Str1.casefold() print('First Output after casefold() is = ', Str2) # Observe the Difference between Original and casefold String print('Original String is = ', Str1) print('Second Output String is = ', Str1.casefold()) # Use casefold() function directly Str3 = 'LearN pYTHON proGramminG'.casefold() print('Third Output after casefold() method is = ', Str3) Str4 = 'pyTHon ProGRAmming 1234 tuTorIal'.casefold() print('Fourth Output after casefold() method is = ', Str4)
First Output after casefold() is = learn python programs at tutorial gateway Original String is = leaRn PYTHON PrOGRAms AT tutOrial gateWay Second Output String is = learn python programs at tutorial gateway Third Output after casefold() method is = learn python programming Fourth Output after casefold() method is = python programming 1234 tutorial
Python string casefold Example 2
This string 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 casefold. Next, we used the casefold String function inside the If Statement. Please refer to the lower function 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 example of a string casefold function is the same above. However, this time we are allowing the user to enter their own strings. Next, we compare those strings with casefold.
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')