π Day 25/150 – Check Alphabet, Digit, or Special Character in Python
This is a very practical problem that helps you understand character classification in Python. It’s commonly used in input validation, password checking, and text processing.
π Goal
Given a character, determine whether it is:
- π€ Alphabet (A–Z, a–z)
- π’ Digit (0–9)
- ⚡ Special Character (anything else like @, #, $, etc.)
πΉ Method 1 – Using if-elif-else
char = 'A' if char.isalpha(): print("Alphabet") elif char.isdigit(): print("Digit") else: print("Special Character")
π§ Explanation:
- isalpha() → checks if character is a letter
- isdigit() → checks if it’s a number
- Anything else → special character
π Best for: Clean and beginner-friendly logic
πΉ Method 2 – Taking User Input
char = input("Enter a character: ")
if char.isalpha():
print("Alphabet")
elif char.isdigit():
print("Digit")
else:
print("Special Character")
π§ Explanation:
- Makes program interactive
- Works for real-time inputs
π Best for: Practical use
πΉ Method 3 – Using Function
def check_char(c): if c.isalpha(): return "Alphabet" elif c.isdigit(): return "Digit" else: return "Special Character" print(check_char('@'))
π§ Explanation:
- Function makes code reusable
- Returns result instead of printing
π Best for: Modular code
πΉ Method 4 – Using Lambda Function
check = lambda c: "Alphabet" if c.isalpha() else "Digit" if c.isdigit() else "Special Character" print(check('5'))
π§ Explanation:
- One-line compact logic
- Uses nested conditional expressions
π Best for: Short expressions
⚡ Key Takeaways
- isalpha() → Alphabet
- isdigit() → Digit
- Else → Special Character
- Always validate input length
π‘ Pro Tip
Try extending this:
- Count alphabets, digits, and symbols in a string
- Build a password strength checker
- Analyze text data
.png)

0 Comments:
Post a Comment