python中string.casefold和string.lower区别
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, lower()
would do nothing to 'ß'
; casefold()
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 [4] converted to lowercase.
The lowercasing algorithm used is described in section 3.13 of the Unicode Standard
参考
https://docs.python.org/3/library/stdtypes.html#str.casefold
https://segmentfault.com/q/1010000004586740/a-1020000004586838
总结
汉语 & 英语环境下面,继续用 lower()
没问题;要处理其它语言且存在大小写情况的时候再用casefold()
最新文章
- Ajax作用、及Ajax函数的编写
- 使用新浪云 Java 环境搭建一个简单的微信处理后台
- javascript练习-定义子类
- php设计模式 策略模式
- 【读书笔记】iOS-引用计数
- BZOJ1580 : [Usaco2009 Hol]Cattle Bruisers 杀手游戏
- P2679 子串
- MySQL 命令杂记
- UVa 10054 (打印欧拉回路) The Necklace
- Signs of a poorly written jQuery plugin 翻译 (Jquery插件开发注意事项,Jquey官方推荐)
- CAN总线与RS485的比较
- VB.NET版机房收费系统---导出Excel表格
- vc++笔记十一
- HTML的标签使用
- PushSharp的使用
- My Upload.com product submission has been approved
- Java第二次作业程序设计作业
- Python中的__init__和__new__
- python之路-变量
- php生成迷宫和迷宫寻址算法实例
热门文章
- PostgreSQL Replication之第十章 配置Slony(6)
- Leetcode: Guess Number Higher or Lower
- Lintcode: Previous Permuation
- MySQL部分1
- 夺命雷公狗—angularjs—14—$location的作用
- 解决xfce4桌面图标消失的问题
- 使用sublime text3的一些事
- Inside TSQL Querying - Chapter 2. Physical Query Processing
- HTML5动画软件工具编辑器 HTML5动画分类 工具推荐
- 天气预报接口api(中国天气网)