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
- Read a character ch.
- If the input is not exactly one character long, print an error and stop.
- If ch is between '0' and '9', it is a digit.
- Else if ch is between 'a' and 'z', it is a lowercase letter.
- Else if ch is between 'A' and 'Z', it is an uppercase letter.
- Else it is a special character.
- Print the result.
Program
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
Enter a character: 7
'7' is a digitA lowercase letter
Enter a character: m
'm' is a lowercase characterA special character
Enter a character: @
'@' is a special characterRecorded 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.chis a string, so this raises aTypeError. Compare with the characters"0"and"9".Writing separate
ifstatements instead ofelif. Then more than one block can run for the same input, and the finalelseattaches only to the lastif.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 ifas in C. Python's keyword iselif.
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
elseblock 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 andord("a")is 97.Which string methods can replace the range checks?
isdigit(),islower()andisupper(). For example"7".isdigit()is True.Can Python chain comparisons like "a" <= ch <= "z"?
Yes.
x <= y <= zmeansx <= y and y <= z, withyworked out only once.