开发者

Python: string.uppercase vs. string.ascii_uppercase

This might be a stupid question but I don't understand what's the difference between string.uppercase and string.ascii_uppercase in the string module. Printing the docstring of both the function prints same thing. Even开发者_如何学Go the output of print string.uppercase and print string.ascii_uppercase is same.

Thanks.


For Python 2.7, the difference is: (see https://docs.python.org/2.7/library/string.html )

string.ascii_uppercase:

  • The uppercase letters 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'. This value is not locale-dependent and will not change.

string.uppercase:

  • A string containing all the characters that are considered uppercase letters. On most systems this is the string 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'. The specific value is locale-dependent, and will be updated when locale.setlocale() is called.

In Python 3+ string.uppercase does not exist as a global "constant" anymore. ( https://docs.python.org/3/library/string.html )


Note that in Python 3.x string.uppercase and also string.lowercase and string.letters have vapourised. It was not considered good practice to have global 'constants' that varied when you changed locale. (See http://docs.python.org/py3k/whatsnew/3.0.html)

So the best thing for any new code is probably to pretend they don't exist in Python 2.x either. That way you don't store up problems if you ever do need to migrate to Python 3.x


On Python 3.x which doesnt have string.uppercase or string.ascii_uppercase, similar functionality can be obtained by using [:upper:] from the regex package, and add regex.UNICODE to be more like string.uppercase, i.e. uppercase characters any language (i.e. not only the current locale).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜