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.

Was this post helpful?

Leave a Reply

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