Check number is integer in Python

In this post, we are going to learn how to Check number is an integer in Python by using some built-in functions.We will ask user to input a number and check the type of input based on that return True or False.

5 Ways to Check number is integer in Python


  • type() :use type() function to check input numer is int or float
  • isinstance(): use to check if number is int or float
  • is_integer() : check float input is integer
  • check if input number is integer using Regular Expression
  • check string number is integer

1. Check input number is integer using type() function


In this example we are using built-in type() function to check if input number is integer or float.The type function will return the type of input object in python.

input_num = eval(input("Please Enter input :"))

if type(input_num) ==int:
    print("Number is integer:",type(input_num))
    
if type(input_num) ==float:
    print("Number is not integer",type(input_num))

Output

Please Enter input :112
Number is integer: <class 'int'>

2. Check number is integer using isinstance()


Python inbuilt isinstance() function check if number has type integer.The isinstance() function return true if given object of specified type.In this example we are checking if input_num object is of specified type int,float.

input_num = eval(input("Please Enter input :"))

print("Number is integer:",isinstance(input_num,int))
    
print("Number is float:",isinstance(input_num,float))

Output

Please Enter input :45
Number is integer: True
Number is float: False

3. check float number is integer using is_integer()


The is_integer() function return true if float value is finite with integer value otherwise return false.

input_num = eval(input("Please Enter float input :"))

print("Number is integer:",input_num.is_integer())

Output

Please Enter float input :45.9
Number is integer: False

4. Check input number is integer Using Regex


In this Python program example, we have defined a regular expression to check if input number is integer.The regular expression check the input number and return true and false.

  • If input number is integer it will return true
  • if input number is not integer it will return False.
import re

input_num = input("Please Enter input :")

reg_exp = re.compile(r'^\-?[1-9][0-9]*$')
is_int = re.match(reg_exp,input_num)

if is_int:
    print("Number is integer:")
else:
    print("Number is not integer")

Output

Please Enter input :45.9
Number is not integer

5. Check string number is integer


Sometimes we have an integer number as a string. In this python code example we are checking if the input string number is integer.

  • First we have converted input value to float using the float() function.
  • Secondly used the is_integer() function to check if it is integer.

def check_str_isInt(input_num):
    try:
        float(input_num)
    except ValueError:
        return False
    else:
        return float(input_num).is_integer()

input_num = input("Please Enter input :")

print(check_str_isInt(input_num))

Output

Please Enter input :56.7
False

Please Enter input :'34'
False

Summary

In this post we have learned multiple ways of how to Check number is integer in Python using built in function type(),is_integer(),isinstance()