String constants (was "Looping over a list question")

Tim Chase python.list at tim.thechases.com
Tue Oct 3 16:16:28 EDT 2006


> I'd also love to see string constants implemented some day too
> (like str.whitespace and str.ascii_letters).

You mean like the "string" module provides? :)

 >>> import string
 >>> print '\n'.join(["%s -> %s" % (s, repr(eval('string.%s' % 
s))) for s in dir(string) if isinstance(eval('string.%s' % s), 
basestring) and not s.startswith('_')])

ascii_letters -> 
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
ascii_lowercase -> 'abcdefghijklmnopqrstuvwxyz'
ascii_uppercase -> 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
digits -> '0123456789'
hexdigits -> '0123456789abcdefABCDEF'
letters -> 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
lowercase -> 'abcdefghijklmnopqrstuvwxyz'
octdigits -> '01234567'
printable -> 
'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$
%&\'()*+,-./:;<=>?@[\\]^_`{|}~ \t\n\r\x0b\x0c'
punctuation -> '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'
uppercase -> 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
whitespace -> '\t\n\x0b\x0c\r '
 >>> string.lowercase
'abcdefghijklmnopqrstuvwxyz'

(you mentioned liking list comprehensions, so that ugly hack of a 
  one-liner extracts all the string properties of the "string" 
module that don't begin with an underscore)

-tkc







More information about the Python-list mailing list