Sai A Sai A
Updated date May 15, 2023
In this blog,we will explain multiple methods to check if an input is an integer in Python. It covers various methods like using type() function, isdigit() method, try-except block, and regular expressions.

Introduction:

In Python, there are various data types available like integers, floating-point numbers, strings, lists, etc. While working with Python, we often come across situations where we need to check if a given input is an integer or not. In this blog post, we will discuss multiple methods to check if an input is an integer in Python.

Method 1: Using type() function

The simplest way to check if a given input is an integer or not is by using the type() function. The type() function returns the type of the variable passed as an argument. We can use the type() function to check if the input is of type int or not.

# Python program to check if input is integer
input_var = input("Enter a number: ")
if type(input_var) == int:
    print("Input is an integer")
else:
    print("Input is not an integer")

Output:

Enter a number: 10
Input is not an integer

In the above example, we first take input from the user using the input() function. We then use the type() function to check if the input is of type int or not. If the input is of type int, we print "Input is an integer". Otherwise, we print "Input is not an integer". In this case, the input is of type str, so the output is "Input is not an integer".

Method 2: Using isdigit() method

Another way to check if a given input is an integer or not is by using the isdigit() method. The isdigit() method is a built-in method in Python that returns True if all the characters in a string are digits. We can use this method to check if the input string contains only digits or not.

# Python program to check if input is integer
input_var = input("Enter a number: ")
if input_var.isdigit():
    print("Input is an integer")
else:
    print("Input is not an integer")

Output:

Enter a number: 10
Input is an integer

In the above example, we first take input from the user using the input() function. We then use the isdigit() method to check if the input string contains only digits or not. If the input string contains only digits, we print "Input is an integer". Otherwise, we print "Input is not an integer". In this case, the input string contains only digits, so the output is "Input is an integer".

Method 3: Using try-except block

Another way to check if a given input is an integer or not is by using a try-except block. We can try to convert the input to an integer using the int() function and catch the ValueError exception if the input is not an integer.

# Python program to check if input is integer
try:
    input_var = int(input("Enter a number: "))
    print("Input is an integer")
except ValueError:
    print("Input is not an integer")

Output:

Enter a number: 10
Input is an integer

In the above example, we first take input from the user using the input() function. We then try to convert the input to an integer using the int() function. If the input is not an integer, the int() function will raise a ValueError exception. We catch this exception using the except block and print "Input is not an integer". Otherwise, we print "Input is an integer". In this case, the input is an integer, so the output is "Input is an integer".

Method 4: Using regular expressions

Regular expressions are a powerful tool in Python that can be used to match patterns in strings. We can use regular expressions to check if a string contains only digits or not. We can use the match() method in the re module to match a regular expression pattern to a string.

# Python program to check if input is integer
import re

input_var = input("Enter a number: ")
if re.match("^[0-9]+$", input_var):
    print("Input is an integer")
else:
    print("Input is not an integer")

Output:

Enter a number: 10
Input is an integer

In the above example, we first import the re module. We then take input from the user using the input() function. We use the match() method in the re module to match a regular expression pattern "^[0-9]+$" to the input string. This regular expression pattern matches strings that contain only digits. If the input string matches the regular expression pattern, we print "Input is an integer". Otherwise, we print "Input is not an integer". In this case, the input string contains only digits, so the output is "Input is an integer".

Conclusion:

In this blog post, we discussed multiple methods to check if an input is an integer in Python. We discussed the following methods:

  • Using type() function
  • Using isdigit() method
  • Using try-except block
  • Using regular expressions

All these methods are simple and easy to understand. However, each method has its own advantages and disadvantages. The choice of method depends on the specific requirements of the problem at hand.

Comments (0)

There are no comments. Be the first to comment!!!