Home » Python Program » Check character is digit using ‘isdigit()’ in Python

Check character is digit using ‘isdigit()’ in Python

Last updated:

Support this post with a reaction:

6,595 views

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:

  1. Start the program.
  2. Read a character from the user using the input() function.
  3. Check whether the input contains exactly one character.
  4. Use the isdigit() method to determine whether the character is a digit.
  5. If the method returns True, display that the given character is a digit.
  6. Otherwise, display that the given character is not a digit.
  7. 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:

Inputisdigit() resultExplanation
"5"TrueContains a digit
"123"TrueAll characters are digits
"A"FalseContains an alphabet
"12A"FalseContains a digit and alphabet
"@"FalseContains a special character
"5.5"FalseContains a decimal point
""FalseEmpty string
" "FalseContains whitespace
"²"TrueSuperscript 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.