Python program to check given input is digit or not

In this tutorial we are going to learn writing program to check if the a given input is Digit ( 0-9) or not. We will also see the explanation on this python program.

Problem statement

For the any input, We have to check a given input character is digit or not.

For example:

Case 1: If user is given input 1

              Output should be “Digit”

              As we know that 1 is digit

Case2: If user is given b or B

             Output should be “Not a Digit”

             As we know that B is a Alphabet not a digit.

Our logic to check given character is digit or not

To check character is digit or not, the character is checked for if it greater than or equal to “0” and smaller than or equal to “9” using if-else statement.

If the specified condition satisfy then the character is digit else it’s not a digit.

  • Firstly , we need to take character as an input from user
  • Then, we will apply the required condition.
  • Finally, we will generate the output based on the return value on condition.

Algorithm to check given character is digit or not

Step1 :  Start

Step2 :   Take input from user

Step3 :   Apply the condition,

                   if ch >= ‘0’ and ch <= ‘9’:

                              Print (‘Digit’)

                     else:

                            Print(’Not a Digit’)

Step4 :  Stop

Python code to check given character is digit or not

Output 1:

Explanation:

Here , the output generated is “ Given Character 8 is a Digit” for input 8 , as the 8>=’0’ and 8<=’9’ , so both condition satisfy well hence the if block of our program is executed.

Output 2:

Explanation:

Here, the output generated is “ Given Character A is not a Digit” for input A (which is a character ), as the A>=’0’ and A<=’9’ , so both condition does not satisfy hence the else block of our program is executed.