How to check if input is integer in python

In python, there are so many types of variables. The given variable should be integer or string. Or else it should be a list, set, or dictionary. In this tutorial, we will learn to check if the entered input is int or not. if the entered input contains a number then it should be integer otherwise it’s something else. By different methods you can check the input is int or not. Let’s learn How to check if the input is an integer in python by giving the below methods.

How to check if input is integer in python

There are different methods that are used to check the input is an integer or not. Let’s check it.

  1. Use int()
  2. You Can Use isnumeric()
  3. Use built-in-type()

From these three methods, you can easily check the input is int or not. Let’s learn this by these methods:

  1. check if the input is an integer in python

    to check if the input is an integer in python just Use int(). By using int() you can check if the input is an integer in python. First of all, you have to enter input and if it is an integer then it gives true, and not then it will give you a false statement. Thus you can check the input is an integer or not. Let's learn this by given below example: userinput = input("Enter the input ") try: int(userinput) its = True except ValueError: its = False print(its) Output : Enter the input 65 True Enter the input aa False

  2. How to check if the input is an integer in python

    to check if the input is an integer in python just use isnumeric(). isnumeric used to check the input is int or not. If the input you entered is a number then it gives you true otherwise it gives false. Let's see it by giving the below example.userinput = input("Enter the input ") print(userinput.isnumeric()) Output : Enter the input 65 True Enter the input aa False

Method 1: Use int()

By using int() you can check if the input is an integer in python. First of all, you have to enter input and if it is an integer then it gives true, and not then it will give you a false statement. Thus you can check the input is an integer or not. Let’s learn this by given below example:

userinput = input("Enter the input ")
try:
    int(userinput)
    its = True
except ValueError:
    its = False
print(its)

Output :

Enter the input 65
True
Enter the input aa
False

Method 2: Just Use isnumeric()

isnumeric used to check the input is int or not. If the input you entered is a number then it gives you true otherwise it gives false. Let’s see it by giving the below example.

userinput = input("Enter the input ")
print(userinput.isnumeric())

Output :

Enter the input 65
True
Enter the input aa
False

Method 3: Use built-in-type()

By using the built-in-type() function you can check the input is int or not. In this method, we will use if-else statements to check the input. After using it we can check if the input is int then it will print integer otherwise it will print string. Let’s learn this by given below example:

def isint(x):
    if type(x) == int:
         return True
    else:
         return False
inpt1 = 54
inpt2 = 'abc'
if isint(inpt1):
    print("Integer")
else:
    print("String")
 
# for input2
if isint(inpt2):
    print("Integer")
else:
    print("String")

Output :

Integer
String

Conclusion

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

Siddharth

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment