python string
Python string casefold() function returns a casefolded copy of the string. This function is used to perform case-insensitive string comparison.
Python字符串casefold()函数返回字符串的casefolded副本。 此函数用于执行不区分大小写的字符串比较。
Python String casefold() (Python String casefold())
Let’s look at a simple example of casefold() function.
让我们看一个简单的casefold()函数示例。
s = 'My name is Pankaj'
print(s.casefold())
s1 = 'Python'
s2 = 'PyThon'
print(s1.casefold() == s2.casefold())
Output:
输出:
my name is pankaj
True
From above program, casefold() function looks exactly same as string lower() function. Actually, it’s effect is same when string is made of ASCII characters.
在上面的程序中,casefold()函数看起来与字符串lower()函数完全相同。 实际上,当字符串由ASCII字符组成时,效果是一样的。
However, casefolding is much more aggressive and it’s intended to remove all case distinctions in a string.
但是,casefolding更具攻击性,它旨在删除字符串中的所有大小写区别。
For example, the German lowercase letter ‘ß’ is equivalent to “ss”. Since it’s already lowercase, lower() would do nothing to ‘ß’ but casefold() will convert it to “ss”.
例如,德语小写字母“ß”等效于“ ss”。 由于已经是小写字母,lower()不会对“ß”起作用,但casefold()会将其转换为“ ss”。
Let’s look at another example to confirm this behavior.
让我们看另一个示例以确认此行为。
s1 = 'ß' # typed with Option+s in Mac OS
s2 = 'ss'
s3 = 'SS'
if s1.casefold() == s2.casefold():
print('s1 and s2 are equals in case-insensitive comparison')
else:
print('s1 and s2 are not-equal in case-insensitive comparison')
if s1.casefold() == s3.casefold():
print('s1 and s3 are equals in case-insensitive comparison')
Output:
输出:
s1 and s2 are equals in case-insensitive comparison
s1 and s3 are equals in case-insensitive comparison
Reference: Official Documentation
参考: 官方文档
翻译自: https://www.journaldev.com/23928/python-string-casefold
python string