Print a range of letters from a-z in Python

avatar

Borislav Hadzhiev

Last updated: Sep 4, 2022

banner

Photo from Unsplash

Print a range of letters from a-z in Python #

Use the string.ascii_lowercase attribute to print a range of letters from a-z, e.g. letters = string.ascii_lowercase. The string.ascii_lowercase attribute returns a string containing the letters from a to z that can be iterated over.

main.py
import string # ✅ print range of letters a-z letters = string.ascii_lowercase print(letters) # 👉️ abcdefghijklmnopqrstuvwxyz # ✅ loop over range of letters for letter in letters: print(letter) # 👉️ a b c d ... 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_of_letters = list(letters) print(list_of_letters) # ------------------------------------------ for i in range(ord('a'), ord('f') + 1): print(chr(i)) # 👉️ a, b, c, d, e, f # ------------------------------------------ # ✅ print a slice of the letter range print(letters[:letters.index('f') + 1]) # 👉️ abcdef

We used the string.ascii_lowercase attribute to get a string containing the letters from a to z.

main.py
import string letters = string.ascii_lowercase print(letters) # 👉️ abcdefghijklmnopqrstuvwxyz

If you need to get a range of uppercase letters from A to Z, use the ascii_uppercase attribute instead.

main.py
import string letters = string.ascii_uppercase print(letters) # 👉️ ABCDEFGHIJKLMNOPQRSTUVWXYZ

You can use string slicing if you need to reverse the letter range.

main.py
import string letters = string.ascii_lowercase print(letters) # 👉️ abcdefghijklmnopqrstuvwxyz reversed_letters = letters[::-1] print(reversed_letters) # 👉️ zyxwvutsrqponmlkjihgfedcba

Use the list() class if you need to convert the letter range to a list.

main.py
import string letters = 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_of_letters = list(letters) print(list_of_letters)

You can use a for loop if you need to iterate over the range of letters.

main.py
import string letters = string.ascii_lowercase for letter in letters: print(letter) # 👉️ a b c d ... x y z

If you need access to the index of the current iteration, use the enumerate() function.

main.py
import string letters = string.ascii_lowercase for index, letter in enumerate(letters): print(index, letter) # 👉️ 0 a, 1 b, 2 c ...

You can also use the range() class to iterate over a range of letters.

main.py
for i in range(ord('a'), ord('f') + 1): print(chr(i)) # 👉️ a, b, c, d, e, f

The ord function takes a string that represents 1 Unicode character and returns an integer representing the Unicode code point of the given character.

main.py
print(ord('a')) # 👉️ 97 print(ord('b')) # 👉️ 98

The chr function is the inverse of ord().

main.py
print(chr(97)) # 👉️ 'a' print(chr(98)) # 👉️ 'b'

It takes an integer that represents a Unicode code point and returns the corresponding character.

You can also use string slicing to get a slice of the letter range.

main.py
import string letters = string.ascii_lowercase print(letters[:letters.index('f') + 1]) # 👉️ abcdef

The syntax for string slicing is my_str[start:stop:step].

The start index is inclusive, whereas the stop index is exclusive (up to, but not including).

Python indexes are zero-based, so the first character in a string has an index of 0, and the last character has an index of -1 or len(my_str) - 1.

We didn't specify a start index, so the string slice starts at index 0.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.