Skip to content
Laviyx Learn

Try

Python Lab · Experiment 7

Check if a character is a digit, lowercase, uppercase or special character

PythonWeek 2JNTUH R22 · CS206ES

Aim

To write a Python program that reads one character and uses an if-elif-else ladder to tell whether it is a digit, a lowercase letter, an uppercase letter or a special character.

Algorithm

  1. Read a character ch.
  2. If the input is not exactly one character long, print an error and stop.
  3. If ch is between '0' and '9', it is a digit.
  4. Else if ch is between 'a' and 'z', it is a lowercase letter.
  5. Else if ch is between 'A' and 'Z', it is an uppercase letter.
  6. Else it is a special character.
  7. Print the result.

Program

Python
ch = input("Enter a character: ")

if len(ch) != 1:
    print("Please enter exactly one character")
elif "0" <= ch <= "9":
    print(f"'{ch}' is a digit")
elif "a" <= ch <= "z":
    print(f"'{ch}' is a lowercase character")
elif "A" <= ch <= "Z":
    print(f"'{ch}' is an uppercase character")
else:
    print(f"'{ch}' is a special character")

Output

A digit

Output 1
Enter a character: 7
'7' is a digit

A lowercase letter

Output 2
Enter a character: m
'm' is a lowercase character

A special character

Output 3
Enter a character: @
'@' is a special character

Recorded by running this exact program. Typed input appears after each prompt.

How it works

Python writes the "else if" of other languages as elif. The conditions are checked from top to bottom, and only the block of the first true condition runs.

Characters are compared by their Unicode code values. The digits '0' to '9' have codes 48 to 57, 'A' to 'Z' have 65 to 90 and 'a' to 'z' have 97 to 122, and each group is in order, so a range check like "a" <= ch <= "z" works. An uppercase letter such as K is caught by the third test in the same way.

Python also has string methods for this: ch.isdigit(), ch.islower() and ch.isupper(). The range checks are used here to show the ladder clearly. In this program a space counts as a special character.

Common mistakes

  • Comparing with numbers instead of characters, as in 0 <= ch <= 9. ch is a string, so this raises a TypeError. Compare with the characters "0" and "9".

  • Writing separate if statements instead of elif. Then more than one block can run for the same input, and the final else attaches only to the last if.

  • Not checking the length of the input. If the user types ab, the range tests compare the whole string and the answer is wrong.

  • Writing else if as in C. Python's keyword is elif.

Viva questions

Try to answer each one aloud before you open it.

  • What is an if-elif-else ladder?

    A chain of conditions checked one after another from the top. The block of the first condition that is true runs, and the rest are skipped. The else block runs only if none of them is true.

  • How does Python compare two characters?

    By their Unicode code values. For example "a" < "b" is True because 97 is less than 98.

  • What does ord() return?

    The Unicode code value of a single character. ord("A") is 65 and ord("a") is 97.

  • Which string methods can replace the range checks?

    isdigit(), islower() and isupper(). For example "7".isdigit() is True.

  • Can Python chain comparisons like "a" <= ch <= "z"?

    Yes. x <= y <= z means x <= y and y <= z, with y worked out only once.

Learn the concepts

All Python Programming Lab experiments →