Python String casefold() Method
The str.casefold()
method converts all the characters of the string into lowercase and returns a copy of the string.
Case-folding means that the characters are converted to a common case, which is usually lowercase, regardless of the case of the original characters.
Example:
text = "Python IS AWESOME"
# Converts string to lowercase
print(text.casefold())
Output
python is awesome
Syntax
The Syntax of the str.casefold()
function is:
str.casefold()
Here, str
is a string.
Parameters
The str.casefold()
function takes no parameters.
Return Value
The str.casefold()
function returns the copy of a string by converting all the characters of the string into lowercase.
Difference between casefold() method and lower() method.
The case-folding is similar to lowercasing, but it's more aggressive and removes all case distinctions in the string.
For example, the German eszett ß
is equivalent to ss
. Since it's already in lowercase, the lower()
method will not make any conversion and returns the string as-is.
However, the casefold()
method converts the letter ß
to its equivalent character ss
.
s1 = "Python"
s2 = "python"
print("Using Lower:", s1.lower() == s2.lower())
print("Using Casefold:", s1.casefold() == s2.casefold())
s3 = "ß"
s4 = "ss"
print(f"Lower of letter {s3} is {s3.lower()}")
print(f"casefold of letter {s3} is {s3.casefold()}")
print("Using Lower:", s3.lower() == s4.lower())
print("Using Casefold:", s3.casefold() == s4.casefold())
Output
Using Lower: True
Using Casefold: True
Lower of letter ß is ß
casefold of letter ß is ss
Using Lower: False
Using Casefold: True
As you can see, both casefold()
and lower()
produce the same results when used on strings that contain only ASCII characters. However, casefold()
is more aggressive than lower()
when converting non-ASCII characters to their lowercase equivalent.
Example 1: How to use casefold()
method in Python?
The casefold()
method is helpful for case-insensitive string comparison.
For Example:
email = "AdMIN@W3basic.com"
confirm_email = "admin@w3basic.com"
# Converts string to lowercase
if email.casefold() == confirm_email.casefold():
print("Email Addresses are the same")
else:
print("Email Addresses are different")
Output
Email Addresses are the same
Another Example:
s1 = 'Python'
s2 = 'python'
print(s1 == s2)
print(s1.casefold() == s2.casefold())
False
True
Example 2: casefold() method when using german characters
In the first example, the casefold()
method converts the umlaut in Ä
to the lowercase equivalent ä
. The second example converts the acute accent in é
to the lowercase equivalent e
. Finally, in the third example, the method converts the german eszett ß
to the string ss
.
# the method converts the umlaut in Ä to the lowercase equivalent ä
s1 = "Äpfel"
s2 = "äpfel"
print("Comparison without Casefold:", s1 == s2)
print("Comparison using Casefold:", s1.casefold() == s2.casefold())
# the method converts the acute accent in é to the lowercase equivalent e
s3 = "Café"
s4 = "cafe\u0301"
print("Comparison without Casefold:", s3 == s4)
print("Comparison using Casefold:", s3.casefold() == s4.casefold())
# the method converts the eszett (ß) to the string ss
s5 = "ß"
s6 = "ss"
print("Comparison without Casefold:", s5 == s6)
print("Comparison using Casefold:", s5.casefold() == s6.casefold())
Output
Comparison without Casefold: False
Comparison using Casefold: True
Comparison without Casefold: False
Comparison using Casefold: False
Comparison without Casefold: False
Comparison using Casefold: True
Reference: Python Official Docs