20

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?

4
  • @Asad yes it would. why wouldn't it? Commented Apr 23, 2014 at 19:50
  • @Asad what compiiler do you use? I have gcc (Ubuntu 4.8.2-19ubuntu1) 4.8.2. It works. Very well.
    – enedil
    Commented Apr 23, 2014 at 19:53
  • @RyanHaining There's a space there. I'm no C expert, but I don't think that would work unless the input was 35. edit: Just looked up scanf, guess I was wrong. Commented Apr 23, 2014 at 20:04
  • @Asad it works with a space, not without. Compile it yourself and test.
    – enedil
    Commented Apr 23, 2014 at 20:06

5 Answers 5

41

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
9
  • 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
    Commented Apr 23, 2014 at 20:45
  • input() still didn't work for me in Pycharm ( Python 2.7 ). Change it to raw_input to make it work. Commented Sep 24, 2015 at 21:08
  • @blumonkey: this question is about Python 3. For Python 2, you indeed need to use raw_input. Commented Sep 24, 2015 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
    Commented May 19, 2017 at 6:25
  • @deppfx: input() can't take multiple lines. Use a loop. Commented May 19, 2017 at 7:51
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())
1
  • 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
    Commented Oct 15, 2017 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)
2
  • 1
    This prompts for input in 2 separate lines. The question was entering 2 integers in 1 line. Commented Sep 3, 2021 at 9:10
  • it is asking for one line of input not one line of code :) Commented Sep 3, 2021 at 19:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.