How do I check if raw input is integer in Python 3?

In Python 3, the input() function always returns a string, even if the user enters a number. To check if the user input is an integer, you can use several approaches. Here are the most effective methods to validate integer input.

Using Try-Except Block (Recommended)

The most robust approach uses a try-except block to attempt converting the input to an integer ?

user_input = input("Enter an integer: ")

try:
    integer_value = int(user_input)
    print("User input is an integer:", integer_value)
except ValueError:
    print("User input is not an integer")
Enter an integer: 45
User input is an integer: 45

This method handles negative numbers and leading/trailing whitespace correctly ?

test_inputs = ["42", "-15", "  23  ", "12.5", "abc"]

for test_input in test_inputs:
    try:
        integer_value = int(test_input)
        print(f"'{test_input}' is an integer: {integer_value}")
    except ValueError:
        print(f"'{test_input}' is not an integer")
'42' is an integer: 42
'-15' is an integer: -15
'  23  ' is an integer: 23
'12.5' is not an integer
'abc' is not an integer

Using isdigit() Method (Limited)

The isdigit() method checks if all characters are digits, but has limitations ?

user_input = input("Enter an integer: ")

if user_input.isdigit():
    print("User input is an integer")
else:
    print("User input is not an integer")
Enter an integer: 99
User input is an integer

Limitation: isdigit() fails with negative numbers ?

test_inputs = ["42", "-15", "12.5", "abc"]

for test_input in test_inputs:
    if test_input.isdigit():
        print(f"'{test_input}' passes isdigit() test")
    else:
        print(f"'{test_input}' fails isdigit() test")
'42' passes isdigit() test
'-15' fails isdigit() test
'12.5' fails isdigit() test
'abc' fails isdigit() test

Using Regular Expressions

Regular expressions can handle positive and negative integers ?

import re

user_input = input("Enter an integer: ")

if re.match(r"^-?\d+$", user_input):
    print("User input is an integer")
else:
    print("User input is not an integer")
Enter an integer: -123
User input is an integer

Comparison of Methods

Method Handles Negative Numbers Handles Whitespace Best For
try-except Yes Yes Most reliable validation
isdigit() No No Simple positive integer check
Regular Expression Yes (with pattern) No Pattern-based validation

Complete Validation Function

Here's a reusable function that combines validation with user-friendly prompting ?

def get_integer_input(prompt):
    while True:
        user_input = input(prompt)
        try:
            return int(user_input)
        except ValueError:
            print("Please enter a valid integer.")

# Usage example
number = get_integer_input("Enter an integer: ")
print(f"You entered: {number}")
print(f"Type: {type(number)}")
Enter an integer: abc
Please enter a valid integer.
Enter an integer: 42
You entered: 42
Type: <class 'int'>

Conclusion

Use the try-except approach for robust integer validation as it handles negative numbers, whitespace, and provides the actual integer value. The isdigit() method works only for positive integers, while regular expressions offer pattern-based validation but require more complex patterns for comprehensive checking.

Updated on: 2026-03-24T16:51:46+05:30

11K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements