The Python casefold is a built-in String function that converts all the characters in a given string into Lowercase letters. Though this is the same as the lower function, casefold is aggressive and stronger than lower.
This casefold function is very useful when we are comparing the strings. In this section, we explain to you how to write string casefold Function in Python Programming with example. The syntax of this 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)
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')
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')