Automatically Generate A List With The English Alphabet In Python
Sometimes you want to get your hands on all the letters in the english alphabet. Here's how:
Lowercase
You can generate a list of with the full alphabet of lowercase english letters using:
import string
alphabet_lowercase = list(string.ascii_lowercase)
print(alphabet_lowercase)
That Outputs:
['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']
Uppercase
Uppercase letters can be had with:
import string
alphabet_uppercase = list(string.ascii_uppercase)
print(alphabet_uppercase)
Which outputs:
['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']
How It Works
The string.ascii_lowercase
and string.ascii_uppercase
calls each return single strings with their correspondingly cased alphabets. abcdefghijklmnopqrstuvwxyz
and ABCDEFGHIJKLMNOPQRSTUVWXYZ
, respectively.
Wrapping a string in list()
splits each character out individually and returns it as a separate item in a list. So, calling list(string.ascii_lowercase)
is equivalent to calling list('abcdefghijklmnopqrstuvwxyz')
which does the split and generates our list ['a', 'b', 'c', ...etc ]
string.ascii_lowercase
and string.ascii_uppercase
are "String Constants". Basically, built-in stuff that does't change (even it the locale does).
There are a few other String Constants for digits and punctuation that you can check out in the docs.