string.casefold和
string.lower
DifferencePython 3.3 introduces a
string.casefold
method whose effect is very similar to that of turning a
string.lower
string into lowercase, so what's the difference between them? Their respective application scenarios?
when Unicode is used casefold
string.casefold
Official Note:
Casefolding is similar to lowercasing and more aggressive because it's intended to remove all case distinctions in a stri Ng. For example, the German lowercase are ‘ß‘
equivalent to "ss"
. Since It is already lowercase, lower()
would does nothing to ‘ß‘
; casefold()
Converts it to "ss"
.
The casefolding algorithm is described in sections 3.13 of the Unicode standard
lower()
‘A-Z‘
It is only valid for ASCII, but in some other languages there is no way to get a lowercase case. The example in the document is the lowercase in German ‘ß‘
‘ss‘
:
s = ‘ß‘s.lower() # ‘ß‘s.casefold() # ‘ss‘
string.lower
Official Note:
Return a copy of the string with all the cased characters [4] converted to lowercase.
The lowercasing algorithm used is described in sections 3.13 of the Unicode standard
Reference
Https://docs.python.org/3/library/stdtypes.html#str.casefold
https://segmentfault.com/q/1010000004586740/a-1020000004586838
Summary
Chinese & English Environment under, continue to use lower()
no problem; To work with other languages and when case is presentcasefold()
String.casefold and String.Lower differences in Python