Python
Alphabet
Programming
Python Tips
Code Tutorial

Alphabet range in Python

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Python does not include a built-in alphabet version of numeric range, but generating letter ranges is straightforward with ord, chr, and ordinary iteration. The main design choice is whether you want a simple ASCII helper for A through Z style work or something more specialized.

The Core Idea: ord and chr

ord converts a character into its numeric code point, and chr converts a code point back into a character. That means you can build an alphabet range by iterating over numbers and converting them back to letters.

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

This is the basic pattern behind most alphabet-range helpers.

Supporting Reverse Order and Step Sizes

If you want range-like flexibility, support a step argument and both forward and backward traversal.

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 more like Python's numeric range, while still working with letters.

Restrict to Real Alphabet Ranges

A raw code-point range can cross punctuation if you mix uppercase and lowercase incorrectly. For example, the characters between Z and a in ASCII are not letters.

That means it is often wise to validate that both boundaries belong to 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')))

This prevents accidental mixed-case or punctuation-heavy output.

Build Lists, Strings, or Dictionaries

Once you have a generator, it composes naturally with normal Python tools.

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)

That flexibility is one reason a generator-style helper works well. The same function can feed many different data structures.

Use string.ascii_lowercase for the Full Alphabet

If you just want all English letters, you may not need a custom range helper at all.

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

This is often the simplest answer when the problem is "give me the alphabet" rather than "give me a subrange from one letter to another."

Spreadsheet Columns Are a Different Problem

People sometimes ask for an alphabet range when they really need spreadsheet-style labels such as A, B, AA, and AB. That is not the same thing as a character range.

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))

If the requirement goes beyond single letters, use a dedicated label function instead of stretching the alphabet-range helper too far.

Unicode and Locale Considerations

For simple English letter ranges, ASCII-based logic is fine. For broader language support, code-point iteration is not enough. Alphabet ordering and what counts as a letter can depend on locale, normalization, and language-specific rules.

So if the real requirement is internationalized alphabet handling, do not assume that a Unicode code-point walk is a correct "alphabet range." That becomes a text-processing and locale problem, not just a Python iteration trick.

Common Pitfalls

One common mistake is forgetting that Python's numeric range excludes the stop value. If you want an inclusive alphabet range, you must adjust the endpoint.

Another pitfall is mixing uppercase and lowercase boundaries and accidentally including punctuation between ASCII blocks.

A third issue is using a single-character helper for spreadsheet labels such as AA and AB, which are a separate problem.

Finally, do not assume code-point order equals natural alphabet order for every language. For internationalized text, stronger tools are needed.

Summary

  • Build alphabet ranges in Python with ord, chr, and range.
  • Add endpoint and step handling deliberately if you want range-like behavior.
  • Validate case or alphabet block boundaries to avoid punctuation surprises.
  • Use string.ascii_lowercase or string.ascii_uppercase when you need the full English alphabet.
  • Treat spreadsheet labels and internationalized alphabets as separate problems from simple ASCII letter ranges.

Course illustration
Course illustration

All Rights Reserved.