How to Create an Alphabet List in Python

AuthorSumit Dey Sarkar

Pubish Date30 Apr 2023

categoryPython

In this tutorial we will learn how to create an alphabet list in Python.

 

How to Create an Alphabet List in Python

 

How to create an alphabet list in Python

You can create an alphabet list in Python using a for loop and the ord() and chr() functions.

 

Here's an example code:

alphabet_list = []
for letter in range(ord('a'), ord('z')+1):
    alphabet_list.append(chr(letter))

This code creates an empty list called alphabet_list. The lowercase ASCII values of all letters from "a" to "z" (inclusive) are then looped over, converted to characters using the chr() function, and added to the list.

 

You can print the alphabet_list to see the result:

print(alphabet_list)

 

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']

 

As an alternative, you can make the alphabet list in one line by using a list comprehension:

alphabet_list = [chr(letter) for letter in range(ord('a'), ord('z')+1)]

This code is shorter but still achieves the same result as the preceding example.

 

Comments 0

Leave a comment