Python casefold method example :
casefold() method is used to convert all characters in a string to lowercase. It doesn’t take any parameter. You can directly call this method to a string. It returns the new lowercase character string.
casefold() method is useful can be used to compare two strings without considering the case. If the string contains both uppercase and lowercase characters, we can use this method and convert them all to lowercase.
Example to convert one string to lowercase using casefold :
str = input("Enter a string : ")
print("Lowercase : {}".format(str.casefold()))
This program will take one user input string, convert it to lowercase using casefold() and print it out.
Sample Output :
Enter a string : Hello World !!
Lowercase : hello world !!
Enter a string : CodeVSCOLOR
Lowercase : codevscolor
Example to compare two strings in python using casefold :
This is one usecase of casefold. We can compare two strings without considering the character case using casefold as like below :
str1 = input("Enter the first string : ")
str2 = input("Enter the second string : ")
if str1.casefold() == str2.casefold() :
print("Both strings are equal")
else :
print("Strings are not equal")
It takes two strings as input from the user and compares them using casefold().
Sample Output :
Enter the first string : CodeVsColor
Enter the second string : codevscolor
Both strings are equal