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

python中string.casefold和string.lower区别的更多相关文章

  1. python中import和from...import...的区别

    python中import和from...import...的区别: 只用import时,如import xx,引入的xx是模块名,而不是模块内具体的类.函数.变量等成员,使用该模块的成员时需写成xx ...

  2. 转发 python中file和open有什么区别

    python中file和open有什么区别?2008-04-15 11:30地痞小流氓 | 分类:python | 浏览3426次python中file和open有什么区别?都是打开文件,说的越详细越 ...

  3. Python中str()与repr()函数的区别——repr() 的输出追求明确性,除了对象内容,还需要展示出对象的数据类型信息,适合开发和调试阶段使用

    Python中str()与repr()函数的区别 from:https://www.jianshu.com/p/2a41315ca47e 在 Python 中要将某一类型的变量或者常量转换为字符串对象 ...

  4. Python中字符串操作函数string.split('str1')和string.join(ls)

    Python中的字符串操作函数split 和 join能够实现字符串和列表之间的简单转换, 使用 .split()可以将字符串中特定部分以多个字符的形式,存储成列表 def split(self, * ...

  5. Python中str()和repr()函数的区别

    在 Python 中要将某一类型的变量或者常量转换为字符串对象通常有两种方法,即 str() 或者 repr() . 区别与使用函数str() 用于将值转化为适于人阅读的形式,而repr() 转化为供 ...

  6. 【转】python中json.loads与eval的区别

    JSON有两种结构: “名称/值”对的集合(A collection of name/value pairs).不同的语言中,它被理解为对象(object),纪录(record),结构(struct) ...

  7. Python中eval与exec用法的区别

    Python中eval,exec这两个函数有着相似的输入参数类型和执行功能,因此在用法上经常出现混淆,以至经常用错,程序易抛出错误.下面主要通过这两个函数的语法来阐述区别,并用例子来进一步说明. 首先 ...

  8. Python中json.dump与repr的区别

    Json是一种轻量级的数据交换格式,Python3 中可以使用 json 模块来对 JSON 数据进行编解码,它包含了两个函数: 引入json包: import json json.dumps(): ...

  9. Python中list,tuple,dict,set的区别和用法

    Python语言简洁明了,可以用较少的代码实现同样的功能.这其中Python的四个内置数据类型功不可没,他们即是list, tuple, dict, set.这里对他们进行一个简明的总结. List ...

随机推荐

  1. spring AutowireCapableBeanFactory 自动注入

    文档:http://docs.spring.io/spring/docs/3.0.x/javadoc-api/org/springframework/beans/factory/config/Auto ...

  2. CGRect 结构体的另外一种写法

    //    _textF = CGRectMake(textX, textY, textSize.width, textSize.height); _textF = (CGRect){{textX, ...

  3. html 标签自己居中

    <div style="width: 200px; height: 200px; border: 1px solid red; margin: 0 auto;">< ...

  4. spfa的SLF优化

    spfa的SLF优化就是small label first 优化,当加入一个新点v的时候如果此时的dis[v]比队首dis[q.front()]还要小的话,就把v点加入到队首,否则把他加入到队尾,因为 ...

  5. 在数组中搜索数据用 filteredArrayUsingPredicate

    苹果官方说明文档:https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Predicates/Articles/ ...

  6. [原创]java WEB学习笔记73:Struts2 学习之路-- strut2中防止表单重复提交

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  7. DataBase: LeetCode

    Combine Two Tables # Write your MySQL query statement below Select p.FirstName, p.LastName, a.City, ...

  8. Android测试AsyncTask下载图片

    package com.example.myact8_async; import org.apache.http.HttpEntity; import org.apache.http.HttpResp ...

  9. mongo聚合和mapreduce例子

    聚合语句-比较集合内两字段大小 db.test.aggregate([ {$match:{"offlineTime":{$gt:ISODate("2016-09-13T0 ...

  10. 如何把一个java工程打成一个jar包(转载)

    1.jar包有入口(即有main()函数) 选中要打包的工程--->右键选择Export---->java----->Runnable java file----->next- ...