Make a list of the Alphabet in Python

avatar

Borislav Hadzhiev

Last updated: Sep 4, 2022

banner

Photo from Unsplash

Make a list of the Alphabet in Python #

To make a list of the alphabet:

  1. Use the string.ascii_lowercase attribute to get a string of the letters in the alphabet.
  2. Use the list() class to convert the string to a list.
  3. The list will contain all of the letters in the alphabet.
main.py
import string # ✅ Get a list of the lowercase letters in the alphabet lower = string.ascii_lowercase list_of_lowercase_letters = list(lower) # 👇️ ['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'] print(list_of_lowercase_letters) # ------------------------------------------- # ✅ Get a list of the UPPERCASE letters in the alphabet upper = string.ascii_uppercase list_of_uppercase_letters = list(upper) # 👇️ ['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'] print(list_of_uppercase_letters)

We used the string.ascii_lowercase attribute to get a string containing the letters from a to z.

main.py
import string lower = string.ascii_lowercase print(lower) # 👉️ abcdefghijklmnopqrstuvwxyz

You can use the list() class to convert the string of characters to a list containing the letters in the alphabet.

main.py
import string lower = string.ascii_lowercase list_of_lowercase_letters = list(lower) # 👇️ ['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'] print(list_of_lowercase_letters)

If you need to get a list of the uppercase letters, use the ascii_uppercase attribute instead.

main.py
import string upper = string.ascii_uppercase list_of_uppercase_letters = list(upper) # 👇️ ['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'] print(list_of_uppercase_letters)

You can use list slicing if you need to reverse the list of letters.

main.py
import string lower = string.ascii_lowercase list_of_lowercase_letters = list(lower)[::-1] # 👇️ ['z', 'y', 'x', 'w', 'v', 'u', 't', 's', 'r', 'q', 'p', 'o', 'n', 'm', 'l', 'k', 'j', 'i', 'h', 'g', 'f', 'e', 'd', 'c', 'b', 'a'] print(list_of_lowercase_letters)

Alternatively, you can use a list comprehension.

Make a list of the Alphabet in Python #

To make a list of the alphabet:

  1. Use the ord() function to get the Unicode code points of the characters a and z.
  2. Use a list comprehension to iterate over the range.
  3. Use the chr() function to get each letter.
main.py
list_of_lowercase_letters = [chr(i) for i in 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'] print(list_of_lowercase_letters) list_of_uppercase_letters = [chr(i) for i in 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'] print(list_of_uppercase_letters)

The ord function takes a string that represents 1 Unicode character and returns an integer representing the Unicode code point of the given character.

main.py
print(ord('a')) # 👉️ 97 print(ord('b')) # 👉️ 98

The chr function is the inverse of ord().

main.py
print(chr(97)) # 👉️ 'a' print(chr(98)) # 👉️ 'b'

It takes an integer that represents a Unicode code point and returns the corresponding character.

We used the range() class to get a range that we can iterate over and used a list comprehension to iterate over the range.

List comprehensions are used to perform some operation for every element or select a subset of elements that meet a condition.

You can use list slicing if you need to get a slice of the list of letters.

main.py
letters = [chr(i) for i in range(ord('a'), ord('z') + 1)] # 👇️ ['a', 'b', 'c', 'd', 'e', 'f', 'g'] print(letters[:letters.index('g') + 1])

The syntax for list slicing is my_list[start:stop:step].

The start index is inclusive and the stop index is exclusive (up to, but not including).

Python indexes are zero-based, so the first item in a list has an index of 0, and the last item has an index of -1 or len(my_list) - 1.

We didn't specify a start index, so the list slice starts at index 0.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.