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!