In this tutorial, you’ll learn how to use Python to make a list of the entire alphabet. This can be quite useful when you’re working on interview assignments or in programming competitions. You’ll learn how to use the string
module in order to generate a list of either and both the entire lower and upper case of the ASCII alphabet. You’ll also learn some naive implementations that rely on the ord()
and chr()
functions.
Using the string Module to Make a Python List of the Alphabet
The simplest and, perhaps, most intuitive way to generate a list of all the characters in the alphabet is by using the string
module. The string
module is part of the standard Python library, meaning you don’t need to install anything. The easiest way to load a list of all the letters of the alphabet is to use the string.ascii_letters
, string.ascii_lowercase
, and string.ascii_uppercase
instances.
As the names describe, these instances return the lower and upper cases alphabets, the lower case alphabet, and the upper case alphabet, respectively. The values are fixed and aren’t locale-dependent, meaning that they return the same values, regardless of the locale that you set.
Let’s take a look at how we can load the lower case alphabet in Python using the string
module:
# Loading the lowercase alphabet to a list
import string
alphabet = list(string.ascii_lowercase)
print(alphabet)
# Returns: ['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']
Let’s break down how this works:
- We import the
string
module - We then instantiate a new variable,
alphabet
, which uses thestring.ascii_lowercase
instance. - This returns a single string containing all the letters of the alphabet
- We then pass this into the
list()
function, which converts each letter into a single string in the list
The table below shows the types of lists you can generate using the three methods:
Instance | Returned List |
---|---|
list(string.ascii_letters) |
['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'] |
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'] |
list(string.ascii_uppercase) |
['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'] |
In the following sections, you’ll learn how to make use of the chr
and ord
functions to generate a list of the alphabet in Python.
Using Python chr and ord to Make a Python List of the Alphabet
In this section, you’ll learn how to make use of the chr and ord functions to generate a list of the alphabet. The chr
function converts an integer value into its corresponding Unicode value. Similarly, the ord
function converts a Unicode value into its integer representation.
Use a For Loop to Make a Python List of the Alphabet
We can use the chr()
function to loop over the values from 97 through 122 in order to generate a list of the alphabet in lowercase. The lowercase letters from a through z are represented by integers of 97 to 122. We’ll instantiate an empty list and append each letter to it. Let’s see what this looks like:
# Generate a list of the alphabet in Python with a for loop
alphabet = []
for i in range(97, 123):
alphabet.append(chr(i))
print(alphabet)
# Returns: ['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']
It’s not always convenient to remember the value of 97 (or 122). Because of this, we can use the ord()
function to determine the integer value of the letter 'a'
and then iterate through 26 additional letters. Let’s see what this looks like:
# Generate a list of the alphabet in Python with a for loop
alphabet = []
start = ord('a')
for i in range(26):
alphabet.append(chr(start + i))
print(alphabet)
# Returns: ['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']
In the next section, you’ll learn how to convert these for loops in Python list comprehensions.
Use a List Comprehension to Make a Python List of the Alphabet
You can convert many Python for loops into list comprehensions, which are much more concise ways of generating lists. For simple for loops, this makes many for loops significantly more readable. List comprehensions save us the trouble of first instantiating an empty list and can be written on a single line of code. Let’s see what list comprehensions looks like:
We can see that we evaluate an expression for each item in an iterable. In order to do this, we can iterate over the range object between 97 and 122 to generate a list of the alphabet. Let’s give this a shot!
# Generate a list of the alphabet in Python with a list comprehensions
alphabet = [chr(value) for value in range(97, 123)]
print(alphabet)
# Returns: ['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']
While our for loop wasn’t very complicated, converting it into a list comprehension makes it significantly easier to read! We can also convert our more dynamic version into a list comprehension, as show below:
# Generate a list of the alphabet in Python with a list comprehension
alphabet = [chr(value) for value in range(ord('a'), ord('a') + 26)]
print(alphabet)
# Returns: ['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']
In the final section, you’ll learn how to use the map()
function to generate a list of the alphabet in Python.
Using Map to Make a Python List of the Alphabet
In this section, you’ll make use of the map() function in order to generate the alphabet. The map function applies a function to each item in an iterable. Because of this, we can map the chr
function to each item in the range covering the letters of the alphabet. The benefit of this approach is increased readability by being able to indicate simply what action is being taken on each item in an iterable.
Let’s see what this code looks like:
# Generate a list of the alphabet in Python with map and chr
alphabet = list(map(chr, range(97, 123)))
print(alphabet)
# Returns: ['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']
Here, we use the map()
function and pass in the chr
function to be mapped to each item in the range()
covering 97 through 123. Because the map()
function returns a map object, we need to convert it to a list by using the list()
function.
Conclusion
In this tutorial, you learned a number of ways to make a list of the alphabet in Python. You learned how to use instances from the string
module, which covers lowercase and uppercase characters. You also learned how to make use of the chr()
and ord()
functions to convert between Unicode and integer values. You learned how to use these functions in conjunction with a for loop, a list comprehension, and the map()
function.
To learn more about the string
module, check out the official documentation here.
Additional Resources
To learn more about related topics, check out the articles listed below:
- Python: Concatenate a String and Int (Integer)
- Python: Sort a String (4 Different Ways)
- Python: Remove Special Characters from a String
Sometimes while working with the alphabet in python, to make our task easy, we want to initialize a list containing all the alphabets. If we do not know how to do it using python, we will manually type all the alphabets, which will be quite a time taking. In this article, we will learn many different ways of initializing a list containing the alphabets in uppercase, lowercase, in both uppercase and lowercase. We will also learn how to do various operations on alphabets.
Python Alphabets are the same as the lower-level C programming language. They have a unique ASCII value attached to them. With the help of these ascii values, you can convert them into characters and numbers. In this post, we’ll go through all the ways to create a list of alphabets.
In every programming language, including python, every alphabet has a unique ASCII value. We can convert those ASCII values into alphabets using chr and ord functions.
Generating a list of Alphabets in Python
There are many ways to initialize a list containing alphabets, and we will start with the naïve way.
General Approach for Python Alphabet
The ASCII value of A-Z lies in the range of 65-90, and the for a-z, the value is in the range 97 – 122. What we will do is that we will run the loop in this range and using chr(), we will convert these ASCII values into alphabets.
A-Z:
# initialize an empty list that will contain all the capital # alphabets alphabets_in_capital=[] for i in range(65,91): alphabets_in_capital.append(chr(i)) print(alphabets_in_capital)
['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-z:
# initialize an empty list that will contain all the lowercase alphabets alphabets_in_lowercase=[] for i in range(97,123): alphabets_in_lowercase.append(chr(i)) print(alphabets_in_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']
Python Alphabet using list comprehension
We can initialize the variable with either ‘a’ or ‘A’ and keep incrementing the ASCII value of the variable. We will run the loop 26 times as there are 26 alphabets in the English language.
var='a' alphabets=[] # starting from the ASCII value of 'a' and keep increasing the # value by i. alphabets=[(chr(ord(var)+i)) for i in range(26)] print(alphabets)
['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']
var='A' alphabets=[] alphabets=[(chr(ord(var)+i)) for i in range(26)] print(alphabets)
['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']
Python Alphabet using map function
We can also use the map function to produce the list of alphabets, let’s see how.
# make a list of numbers from 97-123 and then map(convert) it into # characters. alphabet = list(map(chr, range(97, 123))) print(alphabet)
['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']
alphabets = list(map(chr, range(ord('A'), ord('Z')+1))) print(alphabets)
Importing the String Module
We can also import the string module, and use its functions to make a list of alphabets without any hassle.
import string lowercase_alphabets=list(string.ascii_lowercase) print(lowercase_alphabets) uppercase_alphabets=list(string.ascii_uppercase) print(uppercase_alphabets) alphabets=list(string.ascii_letters) print(alphabets)
['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']
How to check if the character is Alphabet or not in Python
If we want to check if the character is an alphabet or not, we can use the if condition or the built-in function. Let’s see how.
Using If Condition
variable='A' if (variable>='a' and variable<='z') or (variable>='A' and variable<='Z'): print("isalphabet") else: print("not a alphabet")
isalphabet
Using built-in function
We can also use the isalpha() method to check whether the character is an alphabet or not.
variable='g' print(variable.isalpha()) print('1'.isalpha())
True False
How to Convert alphabet into ASCII value
Let us see how we can convert alphabets into their respective ASCII values.
var='G' # using ord() function print(ord(var))
71
Must Read:
- How to Convert String to Lowercase in
- How to Calculate Square Root
- User Input | Input () Function | Keyboard Input
- Best Book to Learn Python
Conclusion
Generally in dynamic programming or while making any application, we need to initialize a Python list containing the alphabets. There are a variety of ways using we can make a list containing all the alphabets like using the string module or by using the ASCII values.
Try to run the programs on your side and let us know if you have any queries.
Happy Coding!
>>> import string
>>> string.ascii_lowercase
'abcdefghijklmnopqrstuvwxyz'
>>> 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']
Alternatively, using range
:
>>> list(map(chr, range(97, 123)))
['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']
Or equivalently:
>>> 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)
....
DATA
ascii_letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
ascii_lowercase = 'abcdefghijklmnopqrstuvwxyz'
ascii_uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
digits = '0123456789'
hexdigits = '0123456789abcdefABCDEF'
octdigits = '01234567'
printable = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~ tnrx0bx0c'
punctuation = '!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~'
whitespace = ' tnrx0bx0c'
Examples of how to create a list of English alphabet letters (from A to Z) with python:
Table of contents
- List of alphabet letters (lowercase)
- List of alphabet letters in (uppercase)
- List of lowercase and uppercase alphabet letters
- Example of application: create a simple coded message
List of alphabet letters (lowercase)
To get a list of English alphabet letters with python, a straightforward solution is to use the library called string
import string
english_alphabet_string_lowercase = string.ascii_lowercase
english_alphabet_string_lowercase
gives
'abcdefghijklmnopqrstuvwxyz'
Then to transform the above string to a list, just use the list() function:
english_alphabet_string_lowercase_list = list(english_alphabet_string_lowercase)
print( english_alphabet_string_lowercase_list )
gives
['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']
Note: if you want to upper case a given letter, b for instance:
english_alphabet_string_lowercase_list[1]
'b'
a solution is to use the method upper():
english_alphabet_string_lowercase_list[1] = english_alphabet_string_lowercase_list[1].upper()
print( english_alphabet_string_lowercase_list )
gives
['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']
List of alphabet letters in (uppercase)
To create a list of alphabet letters in uppercase, we can use a list comprehension:
[l.upper() for l in english_alphabet_string_lowercase]
gives
['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']
or directly from the library sring:
english_alphabet_string_uppercase= string.ascii_uppercase
english_alphabet_string_uppercase
gives
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
then create a list using list() method:
english_alphabet_string_uppercase_list = list(english_alphabet_string_uppercase)
print( english_alphabet_string_uppercase_list )
gives
['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']
Note: same to transform a given letter from uppercase to lowercase
english_alphabet_string_uppercase_list[1]
B
english_alphabet_string_uppercase_list[1].lower()
b
List of lowercase and uppercase alphabet letters
alphabet = list(string.ascii_lowercase) + list(string.ascii_uppercase)
gives
['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']
Example of application: create a simple coded message
Create a list of alphabet letters
a1 = list(string.ascii_lowercase) + list(string.ascii_uppercase)
print( alphabet )
gives
['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']
Rotate elements of the previous list
from collections import deque
a2 = deque( a1 )
a2.rotate(2)
print( list( a2 ) )
gives
['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']
Create a secret dictionary
secret = {}
for letter, coded_letter in zip(a1,a2):
secret[letter] = coded_letter
and a function to encode a word:
def encoder_function(letter):
return secret[letter]
Now let’s encode the word ‘Hello’:
s = 'Hello'
s = list(s)
print( encoder_function(‘a’) )
''.join( list( map(encoder_function, s) ) )
gives
'Fcjjm'
While working with Python lists, sometimes we wish to initialize the list with the English alphabet a-z or A-Z. This is an essential utility in competitive programming and also in certain applications. Let’s discuss various methods to achieve this using Python.
Initialize the list with alphabets using string.ascii_uppercase
The most pythonic and latest way to perform this particular task. Using this new inbuilt function will internally handle the coding part providing a useful shorthand for the user.
Note: You can use lowercase instead of uppercase to generate lower alphabets.
Python3
import
string
test_list
=
[]
test_list
=
list
(string.ascii_uppercase)
print
(
"List after insertion : "
+
str
(test_list))
Output :
List after insertion : [‘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’]
Initialize the list with alphabets using a naive method
The most general method that comes to our mind is using the brute force method of running a Python loop till 26 and incrementing it while appending the letters in the list. Refer to ASCII Table for more.
Python3
test_list
=
[]
print
(
"Initial list : "
+
str
(test_list))
alpha
=
'a'
for
i
in
range
(
0
,
26
):
test_list.append(alpha)
alpha
=
chr
(
ord
(alpha)
+
1
)
print
(
"List after insertion : "
+
str
(test_list))
Output :
Initial list : []
List after insertion : [‘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’]
Initialize the list with alphabets using list comprehension
This is a method similar to the above method, but rather a shorthand for the naive method as it uses the list comprehension technique to achieve the task.
Python3
test_list
=
[]
print
(
"Initial list : "
+
str
(test_list))
test_list
=
[
chr
(x)
for
x
in
range
(
ord
(
'a'
),
ord
(
'z'
)
+
1
)]
print
(
"List after insertion : "
+
str
(test_list))
Output :
Initial list : []
List after insertion : [‘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’]
Initialize the list with alphabets using a map()
Using a map() is an elegant way to perform this particular task. It type casts the numbers in a range to a particular data type, char in this case, and assigns them to the list.
Python3
test_list
=
[]
print
(
"Initial list : "
+
str
(test_list))
test_list
=
list
(
map
(
chr
,
range
(
97
,
123
)))
print
(
"List after insertion : "
+
str
(test_list))
Output :
Initial list : []
List after insertion : [‘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’]
Last Updated :
13 Sep, 2022
Like Article
Save Article