casefold in Python
In this post, you will learn about the casefold() method of Python.
The casefold() method of Python returns a new copy of string where all the characters are in lower case. It is similar to string.lower(), but more aggressive as it can also remove all case distinctions in a string. This method was introduced in Python 3.3. This is generally used for the purpose of caseless comparison, particularly when you need to take characters from many different languages.
Syntax of casefold() methodstring.casefold()
Example1 of casefold()
Here is the basic example of string.casefold(), it will convert all uppercase letters to lowercase.
str1 = "WELCOME to ETutorialsPoint"
casefolded_str = str1.casefold()
print("Original String - ",str1 )
print("Casefolded String - ",casefolded_str )
Output -
Original String - WELCOME to ETutorialsPoint
Casefolded String - welcome to etutorialspoint
Example 2 of casefold()
Here is the other example of casefold() method.
str1 = "hII joHN, hoW ARE YOU?"
# Calling function
str2 = str1.casefold()
# Displaying result
print("Original String:", str1)
print("New String:", str2)
Output -
Original String: hII joHN, hoW ARE YOU?
New String: hii john, how are you?
String comparison using casefold()
Here, we have compared two strings using casefold() method.
str1 = 'Example OF CaseFold'
str2 = 'ExampLE OF CASEFold'
if(str1.casefold() == str2.casefold()):
print('Both strings are equal')
else:
print('Both strings are not equal')
Output
Both strings are equal
casefold() vs lower()
In the above examples, all the process done by casefold() can also be done by lower() method. But, what if the string contains characters from another language or some ASCII code. The Python string casefold() method solves this problem.
In German, there is one ligature (ß). The letter ß, is also known as the "sharp S" or "ss", which is already in lowercase. So, the German letter ß is equivalent to the 'ss'. So, where ever the letter ß is found, the casefold() method convert it into 'ss' but lower() would do nothing to 'ß'. The given example will clarify this concept.
en = 'ss'
gm = 'ß'
if en.casefold() == gm.casefold():
print('Casefolded strings of en and gm are equal')
else:
print('Casefolded strings of en and gm are not equal')
str1 = "abriß"
str2 = "gebiß"
str3 = "große"
print("str1 is ", str1.casefold())
print("str2 is ", str2.casefold())
print("str3 is ", str3.casefold())
Output
Casefolded strings of en and gm are equal
str1 is abriss
str2 is gebiss
str3 is grosse
Related Articles
Convert string to list Python
Convert list to string Python
Remove character from string Python
Greatest common divisor Python recursive
Python String isalpha() Method
Program to print ASCII Value of a character
Python program to sort words in alphabetical order
*args and **kwargs in Python
Printing Simple Diamond Pattern in Python
Stemming and Lemmatization in Python
Python | Generate QR Code using pyqrcode module
Text Detection and Extraction using OpenCV and OCR
Python Random shuffle() Method
Fibonacci Series In Python | Python Program To Print
File Handling in Python
Python - XML to JSON
Python XML to Dictionary
Serialize Python dictionary to XML