博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python中string.casefold和string.lower区别
阅读量:4488 次
发布时间:2019-06-08

本文共 1004 字,大约阅读时间需要 3 分钟。

string.casefold和string.lower 区别

python 3.3 引入了string.casefold 方法,其效果和 string.lower 非常类似,都可以把字符串变成小写,那么它们之间有什么区别?他们各自的应用场景?

对 Unicode 的时候用 casefold

string.casefold官方说明:

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,  would do nothing to 'ß'; converts it to "ss".

The casefolding algorithm is described in section 3.13 of the Unicode Standard

 

lower() 只对 ASCII 也就是 'A-Z'有效,但是其它一些语言里面存在小写的情况就没办法了。文档里面举得例子是德语中'ß'的小写是'ss'

s = 'ß's.lower() #  'ß's.casefold() # 'ss'

string.lower官方说明:

Return a copy of the string with all the cased characters  converted to lowercase.

The lowercasing algorithm used is described in section 3.13 of the Unicode Standard

 

参考

https://segmentfault.com/q/1010000004586740/a-1020000004586838

总结

汉语 & 英语环境下面,继续用 lower()没问题;要处理其它语言且存在大小写情况的时候再用casefold()

 

转载于:https://www.cnblogs.com/zhanmeiliang/p/5988207.html

你可能感兴趣的文章
android 教程实例系列
查看>>
lucene笔记
查看>>
tomcat无法正常shutdown
查看>>
zookeeper + dubbo 搭建
查看>>
根据前序遍历和中序遍历求出二叉树并打印
查看>>
作业4
查看>>
LeetCode "Divide Two Integers"
查看>>
mcs51 串口通信 单片机发 pc收
查看>>
MySQL ACID及四种隔离级别的解释
查看>>
text-align 属性,输入框数字向右靠
查看>>
debian分区方案(就这个看着靠谱点)转
查看>>
虫师Selenium2+Python_11、自动化测试项目实战
查看>>
模拟竖式除法
查看>>
java调用dll
查看>>
图形界面组件实验的一点总结
查看>>
django 1.11.16之环境搭建
查看>>
15.SpringMVC和Spring上下文关系(为什么SpringMVC可以调用到Spring)
查看>>
Syncfusion的社区许可及免费电子书和白皮书
查看>>
JAVA自学作业02
查看>>
蛇形矩阵
查看>>