Python In Hindi 45
String Methods
casefold() vs lower()
Case-Insensitive string comparison
'ß'.casefold()= 'ss'
'ß'.lower() = 'ß'
The CS Pundit
String Method - casefold()
The casefold() method is also a built-in function in Python’s string class. It’s used to convert all
the characters in a string to lowercase, similar to the lower() method. However, casefold() is more
aggressive because it is designed to remove all case distinctions in a string. It’s often used for
caseless matching, i.e., ignores cases when comparing.
Syntax : string.casefold()
Parameters : This method does not take any parameters.
Return Value : It returns a casefolded copy of the string. Case changes are done according to
the Unicode standard.
Basic Usage:
s = "Hello, World!"
print(s.casefold()) # Output: hello, world!
String Method - casefold()
It converts both ASCII and non-ASCII characters to lowercase. This method is particularly useful when you
want to ignore all case distinctions while comparing strings.
For example, the German lowercase letter ‘ß’ is equivalent to “ss”. While lower() would retain ‘ß’,
casefold() converts it to “ss”.
string ="ß"
print("Using lower():", string.lower()) # Output: Using lower(): ß
print("Using casefold():", string.casefold()) # Using casefold(): ss
So, casefold() can be more suitable than lower() for implementing case-insensitive string comparisons.
However, the actual choice between lower() and casefold() depends on the specific requirements of your
program. If you’re dealing with English text, lower() is usually sufficient. If you’re dealing with
multi-language text, casefold() might be the better choice.
casefold() vs lower()
>>>eng1 = 'HOT'
>>>eng2 = 'hot'
>>>eng1==eng2
False
>>>eng1.casefold()==eng2.casefold()
True
>>>eng1.lower()==eng2.lower()
True
casefold() vs lower()
>>>ger1 = 'HEISS' #HOT in English
>>>ger2 = 'heiß' #hot in English
>>>ger1.lower()==ger2.lower()
False
>>>ger1.casefold()==ger2.casefold()
True
>>> g1='ß'
>>> g2='SS'
>>> g1.lower()
'ß'
>>> g2.lower()
'ss'
>>> g1.casefold()
'ss'
>>> g2.casefold()
'ss'
casefold() vs lower()
g='ß'
g.lower()
'ß'
g.casefold()
'ss'
g.upper()
'SS'
g.upper().lower()
'ss'
g.lower()
'ß'
g.upper().lower()==g.lower()
False
g.upper().casefold()==g.casefold()
True
casefold() vs lower()
Sigma
g1='ς'
g1.lower()
'ς'
g1.casefold()
'σ'
g1.upper()
'Σ'
g1.upper().lower()
'σ'
g1.lower()
'ς'
casefold() vs lower() - Official Definition
Official Definition from https://docs.python.org/
● str.casefold()
Return a casefolded copy of the string. Casefolded strings may be used for caseless
matching.
Casefolding is similar to lowercasing but more aggressive because it is intended to remove
all case distinctions in a string. For example, the German lowercase letter 'ß' is equivalent
to "ss". Since it is already lowercase, lower() would do nothing to 'ß'; casefold() converts it
to "ss".
Link : https://docs.python.org/3/library/stdtypes.html#str.casefold
● str.lower()
Return a copy of the string with all the cased characters converted to lowercase.
Link : https://docs.python.org/3/library/stdtypes.html#str.lower
Differences between lower() and casefold()
Differences between lower() and casefold()
● lower() converts only ASCII characters to lowercase, while casefold() converts both ASCII
and non-ASCII characters to lowercase.
● casefold() is appropriate for both case-sensitive and case-insensitive string comparisons,
whereas this is not the case for the lower() method.
● lower() provides higher performance than casefold() , as casefold() works slower than
lower().
● If you’re dealing with English text, lower() is usually sufficient. If you’re dealing with
multi-language text, casefold() might be the better choice.
casefold() vs lower()
import unicodedata
total = 0
for codepoint in range(1114112):# Check all Unicode characters
from 0 to 1114111
char = chr(codepoint)
if char.lower() != char.casefold():
total += 1
print(f"CodePoint : {codepoint} ,Original: {char},
Lower: {char.lower()}, Casefold: {char.casefold()}")
print("Total differences:", total)
Note :Total Unicode characters 1114112 (From 0 to 1114111)
casefold() vs lower()
Output :
CodePoint : 181 ,Original: µ, Lower: µ, Casefold: μ
CodePoint : 223 ,Original: ß, Lower: ß, Casefold: ss
CodePoint : 329 ,Original: ʼn, Lower: ʼn, Casefold: ʼn
CodePoint : 383 ,Original: ſ, Lower: ſ, Casefold: s
CodePoint : 496 ,Original: ǰ, Lower: ǰ, Casefold: ǰ
CodePoint : 837 ,Original: ͅ, Lower: ͅ, Casefold: ι
CodePoint : 912 ,Original: ΐ, Lower: ΐ, Casefold: ΐ
CodePoint : 944 ,Original: ΰ, Lower: ΰ, Casefold: ΰ
CodePoint : 962 ,Original: ς, Lower: ς, Casefold: σ
CodePoint : 976 ,Original: ϐ, Lower: ϐ, Casefold: β
CodePoint : 977 ,Original: ϑ, Lower: ϑ, Casefold: θ
CodePoint : 981 ,Original: ϕ, Lower: ϕ, Casefold: φ
CodePoint : 982 ,Original: ϖ, Lower: ϖ, Casefold: π
Join Telegram Channel For Free PDFs & PPTs
Telegram Channel : https://t.me/TheCSPundit
Youtube Channel : @TheCSPundit