Check if a Given Character is a Digit using isdigit()

A Python program to check if a given character is a digit using the isdigit() method for efficient validation.

Explanation:

Function Definition: Defines a function check_digit(char).
 

Check if Digit: Uses isdigit() to verify if char is a digit (0-9).
 

Return Result:
 

  • Returns "Digit" if input is a digit.
     
  • Returns "Not a Digit" if input is not a digit.
     

User Input:
 

  • Takes input using input().
     
  • .strip() removes any extra spaces before or after the input.
     

Function Call & Output: Calls check_digit(char) and prints the result.

 

Program:

def check_digit(char):

    if char.isdigit():

        return "Digit"

    else:

        return "Not a Digit"

char = input("Enter a character: ").strip()

print(check_digit(char))