In Python, the isdigit() method is used to check whether a character or string contains only digit characters. It is a built-in string method that returns True if all characters in the string are digits and the string is not empty. Otherwise, it returns False.
Problem Statement
Write a Python program to check whether the given input character is a digit or not using the built-in isdigit() method.
Example 1:
Input:
5
Output:
5 is a Digit
Example 2:
Input:
A
Output:
A is Not a Digit
Example 3:
Input:
@
Output:
@ is Not a Digit
What Is the isdigit() Method in Python?
The isdigit() method is a built-in Python string method used to determine whether all characters in a string are digits.
It returns a Boolean value:
True: If the string contains only digit characters and is not empty.False: If the string contains alphabets, spaces, special characters, or a combination of digits and other characters.
Syntax:
string.isdigit()
Logic to Check Whether a Character Is a Digit in Python
We can check whether a character is a digit using the following steps:
- Start the program.
- Read a character from the user using the
input()function. - Check whether the input contains exactly one character.
- Use the
isdigit()method to determine whether the character is a digit. - If the method returns
True, display that the given character is a digit. - Otherwise, display that the given character is not a digit.
- Stop the program.
Python Program to Check Whether a Character Is a Digit Using isdigit()
# Read a character from the user
ch = input("Enter a character: ")
# Check whether the input is a single digit
if len(ch) == 1 and ch.isdigit():
print(ch, "is a Digit")
else:
print(ch, "is Not a Digit")
Output 1:
Enter a character: 5
5 is a Digit
Explanation:
The user enters 5 as input. First, the program checks whether the input contains exactly one character. Then, the isdigit() method checks whether the character is a digit. Since both conditions are satisfied, the if block executes and displays 5 is a Digit.
Output 2:
Enter a character: B
B is Not a Digit
Explanation:
The user enters B as input. The isdigit() method returns False because B is an alphabet, not a digit. Therefore, the else block executes and displays B is Not a Digit.
Output 3:
Enter a character: @
@ is Not a Digit
Explanation:
The input character is @, which is a special character. Since it is not a digit, the isdigit() method returns False, and the program displays @ is Not a Digit.
How Does the isdigit() Method Work in Python?
The isdigit() method checks every character in the given string and determines whether all characters belong to the Unicode digit categories recognized by Python.
Consider the following examples:
| Input | isdigit() result | Explanation |
|---|---|---|
"5" | True | Contains a digit |
"123" | True | All characters are digits |
"A" | False | Contains an alphabet |
"12A" | False | Contains a digit and alphabet |
"@" | False | Contains a special character |
"5.5" | False | Contains a decimal point |
"" | False | Empty string |
" " | False | Contains whitespace |
"²" | True | Superscript digit |
Note: The isdigit() method can recognize certain Unicode digit characters, not just the English digits from 0 to 9. For example, '²'.isdigit() returns True.