To create a list of the letters of the alphabet in Python, the easiest way is to use the string module and access the pre-defined string constants ascii_lowercase, ascii_uppercase, or ascii_letters.
import string
print(list(string.ascii_lowercase))
print(list(string.ascii_uppercase))
print(list(string.ascii_letters))
#Output:
['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']
['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']
['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', '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']
You can also use the chr() function to get a list of letters from their integer representation.
letters = []
for i in range(97, 123):
letters.append(chr(i))
print(letters )
#Output:
['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']
When working in Python, there are many great pre-defined constants that we can use so we don’t need to reinvent the wheel every time we want to create a new application or piece of code.
One example is a list of the letters of the alphabet.
To create a list of the alphabet in Python, you can use the string module pre-defined string constants ascii_lowercase, ascii_uppercase, or ascii_letters.
ascii_lowercase contains the letters of the alphabet and they are all lowercase, while ascii_uppercase has all of the letters but uppercase.
ascii_letters is the combination of ascii_lowercase and ascii_uppercase.
Below shows how you can use the string module pre-defined string constants ascii_lowercase, ascii_uppercase, and ascii_letters to create the alphabet in Python.
import string
print(list(string.ascii_lowercase))
print(list(string.ascii_uppercase))
print(list(string.ascii_letters))
#Output:
['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']
['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']
['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', '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']
Using chr() Function to Create Alphabet in a For Loop
Another way to create the alphabet in Python is with the chr() function. chr() converts an integer to a Unicode character.
The lowercase letters are represented by the integers 97 to 122. Therefore, we can create a loop that loops over 97 to 122 and create the alphabet with the help of chr().
Below is an example of how you can create a list of the letters of the alphabet with chr() and a loop in Python.
letters = []
for i in range(97, 123):
letters.append(chr(i))
print(letters )
#Output:
['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']
Hopefully this article has been useful for you to learn how to create a list of letters and create the alphabet in a list variable in Python.