string – Alphabet range in Python

string – Alphabet range in Python

>>> import string
>>> string.ascii_lowercase
abcdefghijklmnopqrstuvwxyz

If you really need a list:

>>> list(string.ascii_lowercase)
[a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z]

And to do it with range

>>> list(map(chr, range(97, 123))) #or list(map(chr, range(ord(a), ord(z)+1)))
[a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z]

Other helpful string module features:

>>> help(string) # on Python 3
....
DATA
    ascii_letters = abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
    ascii_lowercase = abcdefghijklmnopqrstuvwxyz
    ascii_uppercase = ABCDEFGHIJKLMNOPQRSTUVWXYZ
    digits = 0123456789
    hexdigits = 0123456789abcdefABCDEF
    octdigits = 01234567
    printable = 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!#$%&()*+,-./:;<=>?@[\]^_`{|}~ tnrx0bx0c
    punctuation = !#$%&()*+,-./:;<=>?@[\]^_`{|}~
    whitespace =  tnrx0bx0c
[chr(i) for i in range(ord(a),ord(z)+1)]

string – Alphabet range in Python

In Python 2.7 and 3 you can use this:

import string
string.ascii_lowercase
abcdefghijklmnopqrstuvwxyz

string.ascii_uppercase
ABCDEFGHIJKLMNOPQRSTUVWXYZ

As @Zaz says:
string.lowercase is deprecated and no longer works in Python 3 but string.ascii_lowercase works in both

Leave a Reply

Your email address will not be published. Required fields are marked *