The Python casefold is a built-in String function which converts all the characters in a given string into Lowercase letters. Though this is same as lower function, casefold is aggressive and stronger than lower.
This function is very useful when we are comparing the strings. In this article, we will show you, How to write string casefold Function in Python Programming with example.
The basic syntax of this function in Python Programming Language is as shown below:
Python casefold Example
The following set of examples will 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 function is very useful in string comparison. The below example compare strings with different case characters of same word. First, we compared without using casefold. Next, we used the casefold function inside the If Statement.
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 string casefold function is same above. However, this time we are allowing 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')
Thank You for Visiting Our Blog