19

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 <iostream>
int main() {
    int a, b;
    std::cin >> a >> b;
    return 0;
}

C:

#include <stdio.h>
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 <module>
    a = int(input())
ValueError: invalid literal for int() with base 10: '3 5'

So how to do it?

enedil
  • 1,605
  • 4
  • 18
  • 34

5 Answers5

36

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
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • 3
    +1. This is the nice example of why `map` and the like functions should remain in Python 3. I would only add the `, *rest` to the left side to make it more robust. The non-map version with the list comprehension is also possible, but it is not that clean: `a, b, *rest = [int(e) for e in input().split()]`. – pepr Apr 23 '14 at 20:45
  • `input()` still didn't work for me in Pycharm ( Python 2.7 ). Change it to `raw_input` to make it work. – Samuel Bushi Sep 24 '15 at 21:08
  • @blumonkey: this question is about Python 3. For Python 2, you indeed need to use `raw_input`. – Martijn Pieters Sep 24 '15 at 21:25
  • @MartijnPieters Is it possible to take the same inputs from multi lines? Something like `a, b = map(int, input().split("\n"))` ? – deppfx May 19 '17 at 06:25
  • @deppfx: `input()` can't take multiple lines. Use a loop. – Martijn Pieters May 19 '17 at 07:51
  • @MartijnPieters how to use the split() function in the list? – Shantanu Feb 06 '19 at 10:31
  • @ShantanuDwivedi: I'm not sure what you are asking. `str.split()` is a string method, if you have a list of strings, then you need a loop (either an explicit `for` loop over the list, or a list comprehension). – Martijn Pieters Feb 06 '19 at 10:42
  • @MartijnPieters how can i use split() function inside the loop? – Shantanu Feb 06 '19 at 10:44
  • @ShantanuDwivedi: that's too broad to answer in comments, because it depends entirely on what you are splitting and what you are doing with the values. Perhaps you can do some research on this with searching Stack Overflow? There are many, many questions and answers on the subject here. – Martijn Pieters Feb 06 '19 at 11:29
5

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

a, b = map(int, raw_input().split())
Dhruv Bhagat
  • 73
  • 1
  • 8
  • 2
    Sorry guy, the tag said "python 3". Your code is valid in Python 2. input in py3 is exactly raw_input from py2. – enedil Oct 15 '17 at 18:51
0
x,y = [int(v) for v in input().split()]
print("x : ",x,"\ty: ",y)
0

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
-2
x, y = int(input()),  int(input())
print("x : ",x,"\ty: ",y)