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:
That prints:
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:
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:
Building lists, strings and dictionaries
A generator composes with normal Python tools, so one function can feed many data structures:
The full alphabet with string.ascii_lowercase
If you just want all English letters, you do not need a helper:
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:
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.
Related reading
- Alternatives for returning multiple values from a Python function
- Amazon S3 boto - how to delete folder?
- amazon ses smtp python usage
- Amazon SES SMTP with Django
- An example using python bindings for SVM library, LIBSVM
- Anaconda export Environment file
- Anaconda ImportError /usr/lib64/libstdc.so.6 version GLIBCXX_3.4.21' not found
- Anaconda Integration with Cuda 9.0 shows Incompatible Package Error
System design interview problems on this topic
- Design a Web Crawler: solution and walkthrough
- Design a Task Scheduler: solution and walkthrough
- Design a URL Shortener (TinyURL): solution and walkthrough
- Design an API Rate Limiter: solution and walkthrough
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo 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 courseData 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.