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()

最新文章

  1. Ajax作用、及Ajax函数的编写
  2. 使用新浪云 Java 环境搭建一个简单的微信处理后台
  3. javascript练习-定义子类
  4. php设计模式 策略模式
  5. 【读书笔记】iOS-引用计数
  6. BZOJ1580 : [Usaco2009 Hol]Cattle Bruisers 杀手游戏
  7. P2679 子串
  8. MySQL 命令杂记
  9. UVa 10054 (打印欧拉回路) The Necklace
  10. Signs of a poorly written jQuery plugin 翻译 (Jquery插件开发注意事项,Jquey官方推荐)
  11. CAN总线与RS485的比较
  12. VB.NET版机房收费系统---导出Excel表格
  13. vc++笔记十一
  14. HTML的标签使用
  15. PushSharp的使用
  16. My Upload.com product submission has been approved
  17. Java第二次作业程序设计作业
  18. Python中的__init__和__new__
  19. python之路-变量
  20. php生成迷宫和迷宫寻址算法实例

热门文章

  1. PostgreSQL Replication之第十章 配置Slony(6)
  2. Leetcode: Guess Number Higher or Lower
  3. Lintcode: Previous Permuation
  4. MySQL部分1
  5. 夺命雷公狗—angularjs—14—$location的作用
  6. 解决xfce4桌面图标消失的问题
  7. 使用sublime text3的一些事
  8. Inside TSQL Querying - Chapter 2. Physical Query Processing
  9. HTML5动画软件工具编辑器 HTML5动画分类 工具推荐
  10. 天气预报接口api(中国天气网)