Python
Alphabet
Programming
Python Tips
Code Tutorial

Python alphabet range (letters from a to z)

Codemia Knowledge Hub ·

Python has no built-in alphabet version of range. For the full English alphabet, use string.ascii_lowercase or string.ascii_uppercase. For a subrange from one letter to another, iterate over the code points with ord and convert them back with chr.

Letter ranges with ord and chr

ord turns a character into its numeric code point, and chr turns a code point back into a character, so you can walk the numbers and convert each one to a letter:

python
1def alphabet_range(start: str, stop: str):
2    for code in range(ord(start), ord(stop) + 1):
3        yield chr(code)
4
5print(list(alphabet_range('a', 'f')))

That prints:

python
['a', 'b', 'c', 'd', 'e', 'f']

Note the + 1: numeric range excludes the stop value, so an inclusive letter range has to extend the endpoint. This pattern underlies most alphabet-range helpers.

Reverse order and step sizes

For range-like flexibility, accept a step and support both directions:

python
1def alphabet_range(start: str, stop: str, step: int = 1):
2    if len(start) != 1 or len(stop) != 1:
3        raise ValueError('start and stop must be single characters')
4    if step == 0:
5        raise ValueError('step cannot be zero')
6
7    start_code = ord(start)
8    stop_code = ord(stop)
9    end = stop_code + 1 if step > 0 else stop_code - 1
10
11    for code in range(start_code, end, step):
12        yield chr(code)
13
14print(list(alphabet_range('A', 'F')))
15print(list(alphabet_range('F', 'A', -1)))
16print(list(alphabet_range('a', 'z', 2))[:5])

This behaves much like Python's numeric range, but with letters.

Keep both ends in the same case

A raw code-point range can include punctuation if you mix cases. The ASCII characters between Z and a, for example, are not letters. Validate that both boundaries are in the same alphabet block:

python
1import string
2
3LOWER = set(string.ascii_lowercase)
4UPPER = set(string.ascii_uppercase)
5
6def safe_alpha_range(start: str, stop: str):
7    if start in LOWER and stop in LOWER:
8        return alphabet_range(start, stop)
9    if start in UPPER and stop in UPPER:
10        return alphabet_range(start, stop)
11    raise ValueError('start and stop must both be lowercase or both uppercase')
12
13print(''.join(safe_alpha_range('m', 't')))

Building lists, strings and dictionaries

A generator composes with normal Python tools, so one function can feed many data structures:

python
1letters = list(alphabet_range('a', 'e'))
2print(letters)
3
4joined = ''.join(alphabet_range('A', 'D'))
5print(joined)
6
7mapping = {letter: ord(letter) for letter in alphabet_range('x', 'z')}
8print(mapping)

The full alphabet with string.ascii_lowercase

If you just want all English letters, you do not need a helper:

python
1import string
2
3print(string.ascii_lowercase)
4print(list(string.ascii_uppercase))

This is the simplest answer when the question is "give me the alphabet" rather than "give me the letters from one letter to another."

Spreadsheet column labels are a different problem

Labels such as A, B, AA and AB are not a character range. Use a dedicated label function instead of stretching the alphabet helper:

python
1def excel_label(index: int) -> str:
2    if index < 1:
3        raise ValueError('index must be positive')
4
5    result = []
6    while index > 0:
7        index -= 1
8        index, rem = divmod(index, 26)
9        result.append(chr(ord('A') + rem))
10
11    return ''.join(reversed(result))
12
13for i in range(1, 6):
14    print(i, excel_label(i))

Unicode and locale

ASCII logic is fine for simple English letter ranges. For other languages, code-point iteration is not enough: alphabet order and what counts as a letter can depend on locale, normalization and language-specific rules. Do not assume a Unicode code-point walk is a correct "alphabet range"; that is a text-processing and locale problem, not an iteration trick.


Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No course covers this one yet

Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.

Build my course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms