How Can We Help?
Python is a dynamically typed language. It offers quite a large set of functionalities for performing different functions. Lists and strings are the two trendy data types in python. In this article, we will understand how to make a python list of alphabets.
To make a python list of the alphabet, we use the string module like string.ascii_letters, string.ascii_lowercase, and string.ascii_uppercase instances, or using for loop, list Comprehension, or mapping method.
Creating the Alphabet List Manually
No doubt that we can always make the list of alphabet manually, whether by taking inputs in the list or by making a list manually by ourselves.
Example (1)
lower_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']
upper_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']
print(f"The list of lower alphabet is: {lower_alphabet}")
print(f"The list of upper alphabet is: {upper_alphabet}")
Output:
The list of lower alphabet is: [‘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’]
The list of upper alphabet is: [‘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’]
However, python allows us to achieve the same without manually adding all the alphabet.
Using String Module
The string module allows us to access the list of alphabets. The alphabet in English is represented through ASCII codes. The module has several inbuilt functions. They are used to access different characters and perform string operations.
Example (2)
# Import the string module
import string
# Print all the alphabets
print(f"All the ascii characters are: {string.ascii_letters}")
# Print all the lowercase alphabets
print(f"All the lowercase ascii characters are: {string.ascii_lowercase}")
# Print all the uppercase alphabets
print(f"All the uppercase ascii characters are: {string.ascii_uppercase}")
# Storing the list of lowercase alphabets in the lower_alphabet list
lower_alphabet = list(string.ascii_lowercase)
# Print the list of lowercase alphabets
print(f"The list of lower alphabet is: {lower_alphabet}")
# Storing the list of uppercase alphabets in the upper_alphabet list
upper_alphabet = list(string.ascii_uppercase)
# Print all the list of uppercase alphabets
print(f"The list of upper alphabet is: {upper_alphabet}")
Output:
All the ascii characters are: abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
All the lowercase ascii characters are: abcdefghijklmnopqrstuvwxyz
All the uppercase ascii characters are: ABCDEFGHIJKLMNOPQRSTUVWXYZ
The list of lower alphabet is: [‘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’]
The list of upper alphabet is: [‘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’]
Explanation:
- First, we imported the module named string in our code using the import statement of python.
- Next, we printed all the alphabet of English with the help of a string.ascii_letters function. Next, we used the ascii_lowercase and ascii_uppercase functions to access the lowercase and the uppercase alphabets, respectively. We printed them.
- We used the typecasting method to convert the string into a list using the function named list. We first used the function ascii_lowercase to access all the lowercase alphabets, and we used the list function to convert it into a list data type. Similarly, we used the ascii_uppercase function to access the uppercase alphabet and the list function to convert it into a list data type.
- We printed all the lists using the print function.
There is the ordinal value associated with the letters of English. The lowercase English letters start with 97 and end with 122. The uppercase English letters start with 65 and end with 90.
Example (3)
print(ord("a"))
print(ord("z"))
print(ord("A"))
print(ord("Z"))
Output:
97
122
65
90
Make a List of the Alphabet in Python Using For Loop
We can access the character corresponding to the numbers with the help of the chr function of python.
Consider the following implementation to perform the same:
Iterate through 97 to 122 (inclusive) for lowercase English letters and 65 to 90 for uppercase English letters.
Now under each iteration, use the chr function to get the corresponding characters of the string.
Example (4)
# Initialize an empty list for the lower alphabet
lower_alphabet =[]
# Initialize an empty list for the upper alphabet
upper_alphabet =[]
# Iterate from 97 to 122
for i in range(97,123):
# Append the corresponding character
lower_alphabet.append(chr(i))
# Iterate from 65 to 90
for i in range(65,91):
# Append the corresponding character
upper_alphabet.append(chr(i))
# Print the list of lowercase alphabets
print(f"The list of lower alphabet is: {lower_alphabet}")
# Print all the list of uppercase alphabets
print(f"The list of upper alphabet is: {upper_alphabet}")
Output:
The list of lower alphabet is: [‘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’]
The list of upper alphabet is: [‘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’]
Explanation:
- First, we initialized two empty lists named lower_alphabet and upper_alphabet. Next, we iterated from 97 to 123 and appended the value of the chr function of the corresponding numbers into the lower_alphabet list. Similarly, we iterated from 65 to 91 and appended to the upper_alphabet list.
- Finally, we printed the lists using print statements.
A quick tip:
We may not always remember the corresponding ordinal value of the letters. A better programming approach is to start with the ordinal value of ‘a‘ for iteration and run for the next 26 iterations for lowercase alphabets. We can start with the ordinal value of ‘A‘ for uppercase alphabets. We only need to get the ordinal values through the function ord.
Example (5)
# Initialize an empty list for the lower alphabet
lower_alphabet =[]
# Initialize an empty list for the upper alphabet
upper_alphabet =[]
# Access the ordinal number of 'a'
start_lower=ord('a')
# Access the ordinal number of 'A'
start_upper=ord('A')
# Iterate from 97 to 122
for i in range(start_lower,start_lower+26):
# Append the corresponding character
lower_alphabet.append(chr(i))
# Iterate from 65 to 90
for i in range(start_upper,start_upper+26):
# Append the corresponding character
upper_alphabet.append(chr(i))
# Print the list of lowercase alphabets
print(f"The list of lower alphabet is: {lower_alphabet}")
# Print all the list of uppercase alphabets
print(f"The list of upper alphabet is: {upper_alphabet}")
Output:
The list of lower alphabet is: [‘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’]
The list of upper alphabet is: [‘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 the List Comprehension:
This is the extension of the above method we discussed. List comprehension is a shorter way to append elements to a list. Instead of iteration and append, we iterate within the list explicitly and add the elements to the list.
It has two advantages over the conventional loop. First, it is a shorter line of code with only one line of code, and second, it has better readability in the code.
Example (7)
# Use a list comprehension for the lower alphabet
lower_alphabet = [chr(x) for x in range(ord('a'), ord('a')+26)]
# Use a list comprehension for the upper alphabet
upper_alphabet = [chr(x) for x in range(ord('A'), ord('A')+26)]
# Print the list of lowercase alphabets
print(f"The list of lower alphabet is: {lower_alphabet}")
# Print all the list of uppercase alphabets
print(f"The list of upper alphabet is: {upper_alphabet}")
Output:
The list of lower alphabet is: [‘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’]
The list of upper alphabet is: [‘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’]
Use Mapping To Get All The English Alphabet List
Mapping is a popular method among advanced python users. This helps to map elements into some methods.
The function takes two arguments. The first argument is the function or method that we need to apply. At the same time, the second argument is the element that we need to add.
Example (8)
# Use a list comprehension for the lower alphabet
lower_alphabet = list(map(chr, range(97, 123)))
# Use a list comprehension for the upper alphabet
upper_alphabet = list(map(chr, range(65, 91)))
# Print the list of lowercase alphabets
print(f"The list of lower alphabet is: {lower_alphabet}")
# Print all the list of uppercase alphabets
print(f"The list of upper alphabet is: {upper_alphabet}")
Output:
The list of lower alphabet is: [‘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’]
The list of upper alphabet is: [‘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’]
We can also use the mapping slightly differently to achieve similar output. We can make our custom-designed function and then map the elements of the list (integers) into it to get the corresponding alphabet.
Example (9)
# Creating a user-defined function to convert numbers to the corresponding alphabet
def covert_num(n):
return chr(n)
# Creating a list for the lowercase alphabet
l = [int(x) for x in range(97, 123)]
# Creating a list for the uppercase alphabet
u = [int(x) for x in range(65, 91)]
lower_alphabet = list(map(covert_num, l))
upper_alphabet = list(map(covert_num, u))
# Print the list of lowercase alphabets
print(f"The list of lowercase alphabet is: {lower_alphabet}")
# Print all the list of uppercase alphabets
print(f"The list of uppercase alphabet is: {upper_alphabet}")
Output:
The list of lowercase alphabet is: [‘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’]
The list of uppercase alphabet is: [‘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’]
Explanation:
- First, we created a custom function in python named convert_num. The function takes only one parameter named n. Next, we created a list named l with the help of list comprehension. We iterated through 97 to 123 and stored all the integers in between them(including 97 and excluding 123),
- Next, we created another list and named it using list comprehension. We iterated through 65 to 91 and stored all the integers between them(including 65 and excluding 91).
- Next, we used the mapping technique. We gave the first argument the convert_num function and the second the l for the lower alphabet and u for the upper alphabet.
- Finally, we printed the two lists.
Conclusion
In this article, we learned how to create lists of alphabets in python using different methods. We first learned how to append all the alphabet manually into the list. Next, we learned that there are corresponding ordinal values to the alphabet. We can get the corresponding alphabet from the number using the chr function. We used this trick to append the elements into the lists iteratively. We also learned how to use list comprehension to append all the English alphabets into the lists.