Placement Prep

Check Character Type in Python: ord() and String Methods

Two ways to classify a character in Python as uppercase, lowercase, digit, or special: ASCII ord() approach and built-in string methods, with worked examples.

By FACE Prep Team 5 min read
python programming placement-prep character-classification ascii coding-questions string-methods

Python’s ord() function maps any character to its ASCII integer, which makes the four-category classification of uppercase, lowercase, digit, or special character a handful of range checks.

Two clean approaches exist: compare the integer from ord() against known ASCII ranges, or use Python’s built-in string methods (isupper(), islower(), isdigit()). Both produce the same result for standard keyboard characters. The difference matters when you hit Unicode, which placement tests almost never ask about.

The four character categories and their ASCII ranges

ASCII (American Standard Code for Information Interchange) assigns a unique integer to every standard keyboard character. The four-category classification maps directly to four integer ranges:

CategoryCharactersASCII range
Uppercase lettersA-Z65-90
Lowercase lettersa-z97-122
Digits0-948-57
Special characters@, #, $, %, space, etc.everything else

The three ranges do not overlap. Two gaps hold symbols worth knowing.

The first gap sits between digits and uppercase letters. ASCII 58-64 holds :, ;, <, =, >, ?, and @. The second gap sits between uppercase and lowercase. ASCII 91-96 holds [, \, ], ^, _, and the backtick.

That second gap is why character sort order matters in alphabetical sort in Python: uppercase letters have lower ASCII values than lowercase, so 'Z' sorts before 'a' in a raw ASCII comparison.

Python gives you ord() and chr() as the two built-in tools for this. ord() takes a single character and returns its integer. chr() is the reverse: chr(65) returns 'A'. Memorise the three anchors: ord('A') == 65, ord('a') == 97, ord('0') == 48.

Classifying with ord() and ASCII range checks

The algorithm is a direct translation of the table above into an if/elif chain. Three range checks cover the three named categories; the final else captures everything else.

The algorithm steps

  • Input a single character.
  • Call ord(character) to get its ASCII integer.
  • If the integer is between 65 and 90 (inclusive), the character is an uppercase letter.
  • If the integer is between 97 and 122 (inclusive), the character is a lowercase letter.
  • If the integer is between 48 and 57 (inclusive), the character is a digit.
  • If none of the above apply, the character is a special character.

Python implementation (ord approach)

def classify_character(c):
    ascii_val = ord(c)
    if 65 <= ascii_val <= 90:
        return "Uppercase Letter"
    elif 97 <= ascii_val <= 122:
        return "Lowercase Letter"
    elif 48 <= ascii_val <= 57:
        return "Digit"
    else:
        return "Special Character"

char = input("Enter a character: ")
print(f"'{char}' is a: {classify_character(char)}")

Python’s chained comparison (65 <= ascii_val <= 90) is cleaner than two separate boolean conditions and reads exactly like the math. The comparison operators in Python covered in the two-number comparison article apply directly here, just chained together.

For broader Python basics practice, the Python basic programs practice set includes similar single-function exercises at varying difficulty levels.

Classifying with built-in string methods

Python’s string type has three methods that do the same classification without needing the ASCII table:

  • c.isupper() returns True if c is an uppercase letter, False otherwise.
  • c.islower() returns True if c is a lowercase letter, False otherwise.
  • c.isdigit() returns True if c is a digit, False otherwise.

Python implementation (built-in methods)

def classify_character_builtin(c):
    if c.isupper():
        return "Uppercase Letter"
    elif c.islower():
        return "Lowercase Letter"
    elif c.isdigit():
        return "Digit"
    else:
        return "Special Character"

When to use which approach

The two approaches produce identical output for all 95 printable ASCII characters (codes 32-126). The difference appears with Unicode. 'é'.islower() returns True, correctly identifying it as lowercase. The ord() approach returns 233 for 'é', which sits outside 97-122, so it gets labelled as a special character. For placement coding rounds that specify ASCII input, use the ord() approach. Interviewers typically expect it. The built-in approach is more correct for general-purpose code, and both are documented in the Python standard library.

Practice problems with worked solutions

Format: each problem walks through the ASCII-range method step by step.

Problem set

  • Q1: Given the character M, classify it.

    • Step 1: ord('M') = 65 + 12 = 77 (M is the 13th letter of the alphabet, A=65)
    • Step 2: Is 77 in range 65-90? Yes.
    • Answer: Uppercase Letter
  • Q2: Given the character p, classify it.

    • Step 1: ord('p') = 97 + 15 = 112 (p is the 16th letter, a=97)
    • Step 2: Is 112 in range 97-122? Yes.
    • Answer: Lowercase Letter
  • Q3: Given the character 4, classify it.

    • Step 1: ord('4') = 48 + 4 = 52 (0=48, count up 4 steps)
    • Step 2: Is 52 in range 48-57? Yes.
    • Answer: Digit
  • Q4: Given the character $, classify it.

    • Step 1: ord('$') = 36
    • Step 2: Is 36 in range 65-90? No. In 97-122? No. In 48-57? No.
    • Answer: Special Character
  • Q5: Given the character z, classify it. (Boundary case.)

    • Step 1: ord('z') = 97 + 25 = 122 (z is the 26th letter, a=97)
    • Step 2: Is 122 in range 97-122? Yes (inclusive upper boundary).
    • Answer: Lowercase Letter
  • Q6: Given the character A, classify it. (Boundary case.)

    • Step 1: ord('A') = 65
    • Step 2: Is 65 in range 65-90? Yes (inclusive lower boundary).
    • Answer: Uppercase Letter

Boundary cases (Q5 and Q6) are the most common source of off-by-one errors. The ranges are inclusive on both ends: 65 and 90 are both valid uppercase ASCII values, 97 and 122 are both valid lowercase values, 48 and 57 are both valid digit values.

Where this shows up in placement coding rounds

Character classification sits in the “basic programming” tier of most service-tier placement coding tests. In AMCAT Automata rounds, string and character manipulation problems appear in the first section alongside simpler array tasks. TCS NQT’s advanced coding module covers Python basics including character checks. Infosys InfyTQ coding practice includes string classification problems as part of its Python fundamentals track.

For CSE and IT students, the key habit to build is deriving the ASCII value from scratch rather than memorising a table. The reasoning: 'A' starts at 65, each subsequent letter increments by 1, so 'M' (the 13th letter, gap of 12) is 77. That derivation takes three seconds and eliminates lookup errors under exam conditions.

The ord() and chr() pair also shows up in problems about Caesar ciphers, ROT13, and password-strength checkers, which are common second-level variations on the same theme.

The integer-to-character mapping that makes ord() useful here is the same concept running inside LLM tokenizers, where every token in a vocabulary gets an integer ID. TinkerLLM traces that pipeline from basic Python character handling through a running tokenizer in interactive notebooks, at ₹299. If the ord() examples in this article felt mechanical, TinkerLLM is where the same logic gets applied to something that generates text.

Primary sources

Frequently asked questions

What does ord() return in Python?

ord() returns the Unicode code point (integer) of a single character. For standard ASCII characters, this matches the ASCII value: ord('A') returns 65, ord('a') returns 97, ord('0') returns 48. Pass it a string longer than one character and Python raises a TypeError.

Can I use isupper() and islower() instead of ASCII values?

Yes. Python's built-in isupper(), islower(), and isdigit() methods work cleanly for single characters. The key difference is scope: isupper() handles Unicode uppercase letters too, while the ASCII ord() approach works only for the standard 128-character set. For placement test questions that specify ASCII, use ord().

What counts as a special character for this classification?

Any character that is not an uppercase letter (A-Z, ASCII 65-90), a lowercase letter (a-z, ASCII 97-122), or a digit (0-9, ASCII 48-57) falls into the special character bucket. This includes punctuation, symbols like @, #, $, %, whitespace, and control characters.

What is the ASCII value range for digits in Python?

Digits 0-9 have ASCII values 48 through 57, inclusive. The character '0' maps to 48, '1' to 49, and '9' to 57. Verify with ord(): ord('0') == 48 and ord('9') == 57.

Does this classification work for non-ASCII (Unicode) characters?

The ASCII ord() approach classifies any character outside the three ASCII ranges (65-90, 97-122, 48-57) as special. Unicode letters like 'é' (ord value 233) would be labelled special even though they are lowercase letters in French. For Unicode-aware classification, use isupper(), islower(), and isdigit(), which respect the full Unicode character database.

Where do character classification questions appear in placement tests?

AMCAT's Automata and coding sections include string and character manipulation problems. TCS NQT's advanced coding section covers Python basics including character classification. The category appears in basic programming rounds at most service-tier placement drives. Boundary case handling (is 'Z' uppercase? is '9' a digit?) is where many attempts go wrong.

What is chr() and how is it related to ord()?

chr() is the inverse of ord(): it converts an integer back to its corresponding character. chr(65) returns 'A', chr(97) returns 'a', chr(48) returns '0'. Together, ord() and chr() let you navigate the ASCII table programmatically: chr(ord('A') + 1) gives 'B'.

Build AI projects

A self-paced playground for building with LLMs.

TinkerLLM is FACE Prep's sister property. A guided environment for shipping real LLM applications, the kind of project that earns a paragraph on your resume, not a line.

Try TinkerLLM (₹299 launch)
Free AI Roadmap PDF