How do you input an integer in one line 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 <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?

asked Apr 23, 2014 at 19:47

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

984k273 gold badges3872 silver badges3234 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

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

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

answered Sep 3, 2021 at 6:58

2

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    For instance, in C we can do something like this:

    One solution is to use raw_input() two times.

    Another solution is to use split()

    Note that we don’t have to explicitly specify split(‘ ‘) because split() uses any whitespace characters as a delimiter as default.

    One thing to note in the above Python code is, both x and y would be of string. We can convert them to int using another line

    x, y = [int(x), int(y)] # We can also use list comprehension x, y = [int(x) for x in [x, y]]

    Below is complete one line code to read two integer variables from standard input using split and list comprehension

    x, y = [int(x) for x in input().split()]  

    x, y = map(int, input().split())

    This article is contributed by Abhishek Shukla. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above


    How do you input an integer in the same line in Python?

    Syntax :.
    Syntax :.
    input().split(separator, maxsplit) Example :.
    # taking multiple inputs at a time. # and type casting using list() function. x = list(map(int, input("Enter a multiple value: ").split())) ... .
    # taking multiple inputs at a time. x = [int(x) for x in input("Enter multiple value: ").split()].

    How do you input a single line 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 print inputs in one line in Python?

    The closest you can get is: print 'I have', a=input() print 'apples and', p=input() print 'pears. '

    How do I put multiple lines on one line in Python?

    In C++/C user can take multiple inputs in one line using scanf but in Python user can take multiple values or inputs in one line by two methods. Using split() method : This function helps in getting multiple inputs from users. It breaks the given input by the specified separator.

    Chủ đề