Read Integer Input In Python

Python can read different types of inputs from the command line interface, terminal, console, or via GUI. These inputs can be in different types like string, integer, or floating-point. In this tutorial, we will examine how to read integer input in Python.

Read Integer Input with input() Method

The standard way to read input from the console or command-line interface is using the input() method. When the input() method is executed the command prompt waits for the user input. The input() method reads input and returns it as a string. But we require to use it as an integer. The int() method can be used to convert read input into the integer.

a = input()

number = int(a)

Alternatively the input() and int() methods can be used in a single line like below.

number = int(input())

Read Multiple Integer Inputs with input() Method

In some cases, multiple integers can be provided for the input. These input values or integers are provided in a single line. the input() method also used for multiple integer inputs. As the input() method returns the input as a string the multiple values should be parsed or split with the string split() method. The returned list should be iterated with for loop and every value should be converted into the integer with the int() method.

lst=input().split()

integer_list=[]

for x in lst:
   integer_list.add(int(x))

Leave a Comment