Check Letter, Digit, or Neither

Take a character and check if it is a letter, a digit, or neither.

IntermediatePhase 1 - Level 4: Logical Operators & Compound StatementsExample 1 of 10
check-letter-digit-or-neither.py
1# Take character
2ch = input("Enter a character: ")
3
4# Check type
5if ch.isalpha():
6 print("Letter")
7elif ch.isdigit():
8 print("Digit")
9else:
10 print("Neither")

Output

Enter a character: a
Letter

Enter a character: 5
Digit

Enter a character: @
Neither

What's going on

Use string methods to check character type.

Key Concepts:

isalpha(): checks if letter
isdigit(): checks if digit
Otherwise: special character