python string_Python String casefold()

python string

python string casefold

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
GitHub Repository. GitHub存储库中签出更多的Python String示例。

Reference: Official Documentation

参考: 官方文档

翻译自: https://www.journaldev.com/23928/python-string-casefold

python string

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值