Python Programs | IT Developer
IT Developer

Python Programs



Share with a Friend

Python Programs - Conditional Statements

Check if a character is uppercase, lowercase, digit, or special character - Python Program

Example 1 :

ch = input("Enter a character: ") if ch.isupper(): print("Uppercase") elif ch.islower(): print("Lowercase") elif ch.isdigit(): print("Digit") else: print("Special character")

Output

 
OUTPUT 1 :
Enter a character: A
Uppercase 

OUTPUT 2 :
Enter a character: a
Lowercase

OUTPUT 3 :
Enter a character: 1
Digit

OUTPUT 4 :
Enter a character: @
Special character

Example 2 :

import string def classify_character(char): """ Classifies a single character as uppercase, lowercase, digit, or special character. Args: char (str): The character to classify. Returns: str: A description of the character type. """ if len(char) != 1: return "Input must be a single character." if char.isupper(): return f"'{char}' is an UPPERCASE letter." elif char.islower(): return f"'{char}' is a LOWERCASE letter." elif char.isdigit(): return f"'{char}' is a DIGIT." elif char in string.punctuation or char.isspace(): return f"'{char}' is a SPECIAL character (or whitespace)." else: return f"'{char}' is an UNKNOWN type of character." # Get input from the user user_input = input("Enter a single character: ") # Classify and print the result result = classify_character(user_input) print(result)

Output

 
OUTPUT 1 :
Enter a single character: A
'A' is an UPPERCASE letter.

OUTPUT 2 :
Enter a single character: a
'a' is a LOWERCASE letter.

OUTPUT 3 :
Enter a single character: 1
'1' is a DIGIT.

OUTPUT 4 :
Enter a single character: @
'@' is a SPECIAL character (or whitespace).

Explanation

import string:

This line imports the string module, which provides access to useful string constants, including string.punctuation (a string containing all common special characters).

classify_character(char) function:

  • Takes a single argument char, which is the character to be classified.
  • Input Validation: if len(char) != 1:checks if the input is indeed a single character. If not, it returns an error message.
  • isupper(): This built-in string method returns Trueif the character is an uppercase letter, False otherwise.
  • islower(): This built-in string method returns Trueif the character is a lowercase letter, False otherwise.
  • isdigit(): This built-in string method returns Trueif the character is a digit (0-9), False otherwise.
  • char in string.punctuation or char.isspace(): This checks if the character is present in the punctuationset (e.g., !, @, #) or if it's a whitespace character (e.g., space, tab, newline) using char.isspace().
  • else: If none of the above conditions are met, the character is classified as an "UNKNOWN type of character."

 

User Input and Output:

 

  • user_input = input("Enter a single character: ")prompts the user to enter a character.
  • result = classify_character(user_input)calls the function to classify the input.
  • print(result)displays the classification result to the user.