HOWTO · Python

How to Read User Input as Integers in Python

Read user input as integers in Python 3: convert a single value with int(input()), read several integers from one line with split() and map(), and handle invalid input with try-except.

On this page

In Python 3, the input() function always returns a string. To work with the entered value as a number, convert that string with int(). This article shows how to read a single integer, how to handle input that is not a valid integer, and how to read several integers from one line.

This article covers Python 3. Python 2 used raw_input() for the same job and reached its end of life in 2020, so the examples below use input() throughout.

Read One Integer With int(input())

Read the input as a string first, then convert it:

user_input = input("Please enter an integer: ")
integer_value = int(user_input)

print("You entered:", integer_value)
print(type(integer_value))

Output:

Please enter an integer: 42
You entered: 42
<class 'int'>

The conversion and the read are often combined into one line, which behaves exactly the same:

integer_value = int(input("Please enter an integer: "))

If the user enters something that is not an integer, such as hello or 3.5, int() raises a ValueError and the program stops. The next section shows how to handle that case.

Handle Invalid Input With try-except

Wrap the conversion in a try-except block to catch the ValueError and ask again instead of crashing:

while True:
    user_input = input("Please enter an integer: ")
    try:
        integer_value = int(user_input)
        print("You entered:", integer_value)
        break
    except ValueError:
        print("That's not a valid integer. Please try again.")

The loop keeps prompting until the conversion succeeds. Only then does break end the loop.

Output:

Please enter an integer: hello
That's not a valid integer. Please try again.
Please enter an integer: 10
You entered: 10

Validating input this way is a best practice for interactive programs: a typo should cost the user one more prompt, not the whole run.

Read Multiple Integers From One Line

When a program needs several numbers at once, let the user type them on one line separated by spaces. split() breaks the line into string pieces, and map() converts each piece with int():

numbers = list(map(int, input("Enter numbers separated by spaces: ").split()))

print(numbers)
print("Sum:", sum(numbers))

Output:

Enter numbers separated by spaces: 4 8 15 16 23 42
[4, 8, 15, 16, 23, 42]
Sum: 108

A list comprehension does the same job and is just as common:

numbers = [int(piece) for piece in input("Enter numbers separated by spaces: ").split()]

If you need a fixed number of values, unpack the result directly. Here the user must enter exactly two integers:

a, b = map(int, input("Enter two numbers: ").split())

print("a =", a)
print("b =", b)

Output:

Enter two numbers: 3 17
a = 3
b = 17

Entering more or fewer values than expected raises a ValueError, so combine this pattern with the try-except loop from the previous section when the count matters.

What int() Accepts and What It Rejects

int() is more forgiving than many people expect. It ignores leading and trailing whitespace, accepts an optional + or - sign, and allows underscores between digits:

print(int(" 42 "))
print(int("+7"))
print(int("1_000"))

Output:

42
7
1000

It rejects anything that is not a whole-number literal in string form, including "3.5", "12a", and the empty string "" — all of these raise ValueError. If your input can contain decimal values, convert with float() instead, or use int(float(text)) when dropping the fractional part is acceptable.

Conclusion

Reading integers in Python 3 comes down to one pattern: input() gives you a string, and int() turns it into an integer. Use a try-except loop so invalid entries are re-prompted instead of crashing the program, and reach for split() with map() or a list comprehension when you need several integers from a single line.

FAQ

  1. How do I read user input as an integer in Python 3?
    Use int(input()): input() reads a line as a string, and int() converts it to an integer.

  2. How do I read multiple integers from a single line?
    Split the line and convert each piece, for example list(map(int, input().split())) or [int(x) for x in input().split()].

  3. What happens if the user enters a non-integer value?
    int() raises a ValueError. Catch it with a try-except block and prompt again.

  4. Why can’t I do arithmetic directly on the result of input()?
    Because input() returns a string, not a number. Adding a string and an integer raises a TypeError until you convert the string with int().

  5. What is the difference between Python 2 and Python 3 for reading input?
    Python 2 read text input with raw_input(), while Python 3 uses input(). The int() conversion works the same in both, but Python 2 is no longer maintained, so new code should use Python 3.