How to input 2 integers in python

I wonder if it is possible to input two or more integer numbers in one line of standard input. In C/C++ it's easy:

C++:

#include 
int main() {
    int a, b;
    std::cin >> a >> b;
    return 0;
}

C:

#include 
void main() {
    int a, b;
    scanf("%d%d", &a, &b);
}

In Python, it won't work:

enedil@notebook:~$ cat script.py 
#!/usr/bin/python3
a = int(input())
b = int(input())
enedil@notebook:~$ python3 script.py 
3 5
Traceback (most recent call last):
  File "script.py", line 2, in 
    a = int(input())
ValueError: invalid literal for int() with base 10: '3 5'

So how to do it?

asked Apr 23, 2014 at 19:47

How to input 2 integers in python

4

Split the entered text on whitespace:

a, b = map(int, input().split())

Demo:

>>> a, b = map(int, input().split())
3 5
>>> a
3
>>> b
5

answered Apr 23, 2014 at 19:48

Martijn PietersMartijn Pieters

985k274 gold badges3877 silver badges3236 bronze badges

9

If you are using Python 2, then the answer provided by Martijn does not work. Instead,use:

a, b = map(int, raw_input().split())

answered Oct 15, 2017 at 17:57

1

x,y = [int(v) for v in input().split()]
print("x : ",x,"\ty: ",y)

answered Sep 3, 2021 at 17:47

How to input 2 integers in python

In python, every time we use input() function it directly switches to the next line. To use multiple inline inputs, we have to use split() method along with input function by which we can get desired output.

a, b = [int(z) for z in input().split()]
print(a, b)

Input:

3 4

Output:

3 4

answered Nov 28, 2021 at 11:39

How to input 2 integers in python

x, y = int(input()),  int(input())
print("x : ",x,"\ty: ",y)

answered Sep 3, 2021 at 6:58

How to input 2 integers in python

2

Last update on August 19 2022 21:50:48 (UTC/GMT +8 hours)

Python Basic: Exercise-134 with Solution

Write a Python program to input two integers in a single line.

Sample Solution-1:

Python Code:

print("Input the value of x & y")
x, y = map(int, input().split())
print("The value of x & y are: ",x,y)

Sample Output:

Input the value of x & y
 2 4
The value of x & y are:  2 4

Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:

Sample Solution-2:

Python Code:

a, b = [int(a) for a in input("Input the value of a & b: ").split()]
print("The value of a & b are:",a,b)

Sample Output:

Input the value of a & b:  2 4
The value of a & b are: 2 4

Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to calculate the time runs (difference between start and current time)of a program.
Next: Write a Python program to print a variable without spaces between values.

How do you take two integer inputs at a time in Python?

In python, every time we use input() function it directly switches to the next line. To use multiple inline inputs, we have to use split() method along with input function by which we can get desired output.

How do you input two values in Python?

However, Python provides the two methods that help us to take multiple values or input in one line..
# Taking multiple inputs in a single line..
# and type casting using list() function..
x = list(map(int, input("Enter multiple values: "). split())).
print("List of students: ", x).

How do you add multiple integers in Python?

To add two numbers in python we will use the ” + ” operator to add two numbers and then the print is used to get the output. We can see the addition of two numbers is 10.0 in the output. You can refer to the below screenshot for the output.

How do you input integers in Python?

As we know that Python's built-in input() function always returns a str(string) class object. So for taking integer input we have to type cast those inputs into integers by using Python built-in int() function.