• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Get List of Letters in Alphabet with Python

Get List of Letters in Alphabet with Python

May 6, 2022 Leave a Comment

To create a list of the letters of the alphabet in Python, the easiest way is to use the string module and access the pre-defined string constants ascii_lowercase, ascii_uppercase, or ascii_letters.

import string

print(list(string.ascii_lowercase))
print(list(string.ascii_uppercase))
print(list(string.ascii_letters))

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

You can also use the chr() function to get a list of letters from their integer representation.

letters = []

for i in range(97, 123):
    letters.append(chr(i))

print(letters )


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

When working in Python, there are many great pre-defined constants that we can use so we don’t need to reinvent the wheel every time we want to create a new application or piece of code.

One example is a list of the letters of the alphabet.

To create a list of the alphabet in Python, you can use the string module pre-defined string constants ascii_lowercase, ascii_uppercase, or ascii_letters.

ascii_lowercase contains the letters of the alphabet and they are all lowercase, while ascii_uppercase has all of the letters but uppercase.

ascii_letters is the combination of ascii_lowercase and ascii_uppercase.

Below shows how you can use the string module pre-defined string constants ascii_lowercase, ascii_uppercase, and ascii_letters to create the alphabet in Python.

import string

print(list(string.ascii_lowercase))
print(list(string.ascii_uppercase))
print(list(string.ascii_letters))

#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 chr() Function to Create Alphabet in a For Loop

Another way to create the alphabet in Python is with the chr() function. chr() converts an integer to a Unicode character.

The lowercase letters are represented by the integers 97 to 122. Therefore, we can create a loop that loops over 97 to 122 and create the alphabet with the help of chr().

Below is an example of how you can create a list of the letters of the alphabet with chr() and a loop in Python.

letters = []

for i in range(97, 123):
    letters.append(chr(i))

print(letters )


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

Hopefully this article has been useful for you to learn how to create a list of letters and create the alphabet in a list variable in Python.

Other Articles You'll Also Like:

  • 1.  Get Substring Between Two Characters with Python
  • 2.  How to Filter Lists in Python
  • 3.  Remove Duplicates from Sorted Array in Python
  • 4.  Count Number of Files in Directory with Python
  • 5.  Python Turtle Fonts – How to Write Text with Different Fonts in Python
  • 6.  pandas Duplicated – Find Duplicate Rows in DataFrame or Series
  • 7.  Check if Variable is Integer in Python
  • 8.  Convert String to List Using Python
  • 9.  Find First Occurrence in String of Character or Substring in Python
  • 10.  Remove Last Element from List Using Python

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy