Alphabet in Python

In this tutorial, you'll discover the various Python functions you can use to create an alphabet list. These functions can be extremely helpful when preparing for programming contests or interview problems. Using the Python string module, you'll discover how to create a list of all lowercase and uppercase letters in the ASCII alphabet. The basic implementations that depend on the Python built-in ord() and chr() methods are also covered.

Using the String Module to Make a Python List of the Alphabet

Using the Python string module is the quickest and most natural way to create a list of every letter in the alphabet. There is nothing you need to install because the Python string module is a member of the default Python library. Using the instances of the string.ascii letters, string.ascii lowercase, and string.ascii uppercase makes it simple to retrieve a list of all the alphabet's letters.

These instances of the string module return the lower and upper case alphabets, as indicated by their names, and the appropriate lower and upper case alphabets. The values are constant and independent of the locale. Thus, they always provide the same results no matter what locale you specify.

Let's take a look at how we can load the lowercase alphabet in Python using the string module:

Code

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

Using Python chr and ord Functions

In this part, you'll discover how to create an alphabet list using the built-in chr and ord functions. An integer value is transformed into its matching Unicode value using the Python chr function. The ord function does the same by converting a Unicode value back to its integer equivalent.

Build a List of the Alphabet Using a For a Loop

To create a list of the letters in lowercase, we can loop over the integer values from 97 to 122 using the chr() method. Integers ranging from 97 to 122 are used to depict the lowercase letters from a to z. We will add each letter to an empty list that we will create. Check out how this appears:

Code

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

It can be challenging to recall what 97 represents (or 122). This allows us to cycle through the other 26 letters after using the ord() method to obtain the integral value of the alphabet "g". Let's have a look at this.

Code

Output:

['g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']

Use Python List Comprehension to Make a List of the Alphabet

We already know that an expression is evaluated for every item in a given iterable object. To accomplish this, we may construct a Python list of the alphabet by iterating over the Python range object between the numbers 97 and 122. We will do this using list comprehension this time.

Code

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

Even though our for loop wasn't particularly complex, making it a Python list comprehension made it much simpler! We can also turn our extra dynamic version into a Python list comprehension, as shown below.

Code

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

In the next section, you'll discover how to create a Python list of the alphabet using the map() method.

Using the Map Function to Construct a List of the Alphabet

We will use the map() method in this part to generate the alphabet list. Each item in the iterable is passed to the function given to the map function. As a result, one can map the Python chr function to every item of the range object encompassing the alphabetic letters. This method improves readability by clarifying what operation is performed on every item of the iterable.

Let's examine this code's appearance:

Code

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

Here, we give the chr function, which the interpreter will map to every item of the range() object that spans from 97 to 123, to the map() method. Since the map() method gives a map object, you must use the list() method to change it into a list.

Python String isalpha()

When all of the characters of the given string are alphabets, the isalpha() function will return True. If not, False is returned.

The syntax of Python isalpha() function is:

Parameters of isalpha():

isalpha() function does not take any parameters.

Return Value from isalpha()

isalpha() produces the result:

  • True if the given string only contains alphabetic characters (the string can contain lowercase and uppercase).
  • False if any one character of the string is not an alphabet.

Example 1

We will see the working of isalpha()

Code

Output:

All the characters of Javatpoint are alphabets:  True
All the characters of Peter Parker are alphabets:  False
All the characters of Peter2 are alphabets:  False

Example 2

Using the isalpha() function with if-else clauses.

Code

Output:

All the characters of the given string are alphabet
All the characters of the given string are not alphabet





Latest Courses