search

Home  >  Q&A  >  body text

python3.x - python 的 string.casefold 和 string.lower 方法区别?

python 3.3 引入了string.casefold 方法,其效果和 string.lower 非常类似,都可以把字符串变成小写,那么它们之间有什么区别?什么时候该用string.casefold而非string.lower ??

In [5]: name = 'Xu Zhoufeng'

In [6]: name.casefold()
Out[6]: 'xu zhoufeng'

In [7]: cname = 'Yu Dongfeng'

In [8]: cname.lower()
Out[8]: 'yu dongfeng'
伊谢尔伦伊谢尔伦2804 days ago951

reply all(1)I'll reply

  • PHPz

    PHPz2017-04-17 17:23:54

    For Unicode, use casefoldcasefold

    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".

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

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

    总结来说,汉语 & 英语环境下面,继续用 lower()没问题;要处理其它语言且存在大小写情况的时候再用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".

    lower() is only valid for ASCII, that is, 'A-Z', but there is no solution for lower case in some other languages. The example given in the document is that the lower case of 'ß' in German is 'ss' (I don't understand this either): #🎜🎜# rrreee #🎜🎜# In summary, in Chinese & English environments, it is no problem to continue using lower(); when dealing with other languages ​​​​and case-sensitive situations, use casefold(). #🎜🎜# #🎜🎜#https://docs.python.org/3/library/stdtypes.html#str.casefold#🎜🎜#

    reply
    0
  • Cancelreply