Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: string.uppercase vs. string.ascii_uppercase

Tags:

python

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 the output of print string.uppercase and print string.ascii_uppercase is same.

Thanks.

like image 499
Dharmit Avatar asked Feb 09 '11 07:02

Dharmit


2 Answers

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

like image 140
Duncan Avatar answered Oct 23 '22 04:10

Duncan


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 )

like image 25
dkamins Avatar answered Oct 23 '22 04:10

dkamins