Check if input is integer in Python

Check if input is integer in Python

There is wide use of input statements in the programming world. In Python, we use the input() function to take in the input from the user. The data entered could be of any data type provided by Python. Sometimes the data type of the input needs to be checked and specified.

This article focuses on how to check if input is integer in Python.

Using the isdigit() method to check if input is integer in Python.

The isdigit() function, when implemented on the input, returns True if the input of the string is a number containing only the digits 0-9.

However, we should note that this method fails to deliver when the input is a negative number.

The following code uses the isdigit() method to check if input is integer in Python.

The above code provides the following output:

Enter the input : 7
True

Let’s run the sample program for negative number.

Enter the input : -20
False

As you can see, it returned false for negative number.

Using the isnumeric() method to check if input is integer in Python.

The isnumeric() function runs similar to the isdigit() function mentioned in the article above. It provides a True value if the encountered input contains solely the digits 0-9.

Like the isdigit() function, this function is unusable when the input contains any negative values as it automatically displays the False value when a - sign is encountered. Decimals with the dot . sign is also not considered to be numeric values in the isnumeric() function.

The following code uses the isnumeric() method to check if input is integer in Python.

The above code provides the following output:

Enter the input : 7
True

Let’s run the sample program for negative number.

Enter the input : -100
False

As you can see, isnumeric() also returned false for negative number.

Both the isdigit() and isnumeric() functions have similar working and functioning. However, the isnumeric() function differs from the isdigit() function is due to the fact that the isnumeric() function returns a True value even if it encounters any numbers in other languages. This is not the case with the isdigit() function, which displays False.

Using exception handling along with the int() function to check if input is integer in Python.

The int() function is a pre-defined function provided by Python that is utilized to convert a number given in any base to a decimal number. The output is a number. An error is raised if the value that needs to be converted is not an integer and cannot possibly be converted into one. To tackle this, we will also use exception handling along with the int() function.

The exceptions that occur during the course of the program can be handled by using the try...except block in Python.

The following code uses exception handling along with the int() function to check if input is integer in Python.

The above code provides the following output:

Enter the input : 7
True

Let’s run the sample program for negative number.

Enter the input : -130
True

As you can see, this method returned true for negative number.

Using Regular Expression to check if input is integer in Python.

Regular Expression, or simply RE can be utilized to check if input is integer in Python.

We will manually generate a pattern that kicks in and returns True whenever an integer is found in the given input string. Moreover, this custom pattern can be modified to support negative numbers as well.

The re library needs to be imported first in order to properly utilize regular expression in Python.

The following code uses Regular Expression to check if input is integer in Python.

The above code provides the following output:

Enter the input : 7
True

Let’s run the sample program for negative number.

Enter the input : -10
True

As you can see, this method returned true for negative number.

Code Explanation:

  • The re library is imported to the code first.
  • The input is taken from the user by using the input() function.
  • Then, the patter is designed using the re.compile() function.
  • The values are then compared using the re.match() function.
  • The output is displayed and successfully provides the solution to the given problem.
Explanation of the pattern:
  • ^ : denotes the beginning of the string
  • \-? : utilized to indicate that the entered could be positive or negative.
  • [1-9] : denotes the first digit of the number. It has to be a number between 1 and 9, but it cannot be 0.
  • [0-9]* : denotes the following digits after the first digit.
  • $ : denotes the termination of the string.

That’s all about how to check if input is integer in Python.

Related Posts

  • Python check for null
    17 November

    How to check for null in Python

    Table of ContentsHow to check if a variable is NULL in Python?Using the is operator to check if a variable is NULL in Python.Using the if condition to check if a variable is NULL in Python.Using the not equal to operator to check if a variable is NULL in Python. This article demonstrates the different […]

  • Sort dictionary by value in Python
    17 November

    Sort dictionary by value in python

    Table of ContentsHow to sort sort dictionary by value in Python?Using for loop along with the sorted() function.Using the sorted() function to sort dictionary by value in Python.Using operator.itemgetter() function to sort sort dictionary by value in Python.Using lambda function to sort sort dictionary by value in Python.Using the OrderedDict() method to sort sort dictionary […]

  • Get class name in Python
    17 November

    Get class name in Python

    Table of ContentsHow to get the attributes of an object?Get Class name in python using the __class__ attribute Get Class name in python using type() functionConclusion While working with objects in python, several times we need to get their class name. In this article, we will discuss several ways to get the class name of an […]

  • Python print to file
    17 November

    How to Print to File in Python?

    Table of ContentsThe print() function in pythonHow to Print to file in python?Print to file using the file parameter in the print functionPrint to file by redirecting standard outputConclusion We use the print() function in python to print any text to standard output. In this article, we will see how we can use the print() […]

  • Python absolute path
    17 November

    Get Absolute Path in Python

    Table of ContentsWhat is an absolute path to a file? How to get absolute path in PythonUse abspath to get the absolute path to file use pathlib module to get the absolute path to file Conclusion We often use files to store the outputs of our programs in permanent storage. Generally, we work with relative […]

  • Python sort list of lists
    17 November

    Sort List of Lists in Python

    Table of ContentsSort List of Lists Using the sort() Method in PythonUsing the itemgetter() methodUsing Lambda FunctionsSort List of Lists Using the sorted() Function in PythonSort lists of lists on the basis of the length of the inner listsConclusion Several times we use 2-D lists or lists of lists in our programs to represent a […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.