Algorithmsalphabet range check

Check if a Character is an Alphabet

TT
Testlaa Team
May 14, 20262 min read

Programs often need to know whether a character is an English letter (AZ or az). That check powers parsers, passwords, identifiers, and form validation before you ever touch regex libraries.

Why this shows up in the real world

Flight codes like AA123 mix letters and digits—validators classify each slot. Student ID schemes sometimes require a leading letter. Keyboard teaching apps highlight only alphabetic keys pressed. Same primitive: is this slot alphabetic?

Core idea (explained for students)

ASCII trick: compare code points, e.g. ('A' <= ch <= 'Z') or ('a' <= ch <= 'z'). Python also offers ch.isalpha(), but note it returns True for letters outside English (Tamil letters too). If the problem says “English alphabet only,” prefer explicit ASCII ranges or ch.isascii() and ch.isalpha().

Try this in Python

def is_ascii_alpha(ch: str) -> bool:
    return len(ch) == 1 and (("A" <= ch <= "Z") or ("a" <= ch <= "z"))


for c in "A9அ":
    print(c, is_ascii_alpha(c))

Common mistakes

  • Using isalpha() when the spec expects ASCII letters only.
  • Forgetting that ch might be a string of length >1 if you slice wrong.
  • Locale-dependent upper/lower tables—stick to explicit ranges in contests.

Key takeaways

  • Match the spec: English letters vs any Unicode letter.
  • Centralize helpers like is_ascii_alpha(ch) once and reuse.
  • Pair with isdigit() checks when parsing tokens.

Tags:

StringsPythonStudents