15

I am familiar with the input() function, to read a single variable from user input. Is there a similar easy way to read two variables?

I'm looking for the equivalent of:

scanf("%d%d", &i, &j); // accepts "10 20\n"

One way I am able to achieve this is to use raw_input() and then split what was entered. Is there a more elegant way?

This is not for live use. Just for learning..

Martin Thoma
  • 124,992
  • 159
  • 614
  • 958
Alterlife
  • 6,557
  • 7
  • 36
  • 49

9 Answers9

31

No, the usual way is raw_input().split()

In your case you might use map(int, raw_input().split()) if you want them to be integers rather than strings

Don't use input() for that. Consider what happens if the user enters

import os;os.system('do something bad')

John La Rooy
  • 295,403
  • 53
  • 369
  • 502
  • 4
    Note that starting with Python 3.0, `raw_input` was renamed to `input`. (And to get the original behavior of `input`, use `eval(input)`.) – Stephan202 Oct 19 '09 at 11:09
  • ...that should be "apply `eval` to the output of `input`". – Stephan202 Oct 19 '09 at 11:12
  • Yes, the obvious behaviour in 3 is much better, but I wish they had dropped some warnings into 2.6 about those changes. I guess I need to study the upgrade guide – John La Rooy Oct 19 '09 at 11:23
  • 1
    no it should be `int(input()`, if you do `eval(input())` in python3 you get a very dangerous backdoor because the user will be able to execute any python statement (even os.system) with your user's rights. – dalloliogm Oct 19 '09 at 11:39
  • 2
    @dialloliogn: read again. I made a *general* remark about how to get Python 2's `input` behavior in Python 3. – Stephan202 Oct 19 '09 at 14:00
  • How can I read multiple variables using this? Eg: I am trying to read 4 variable inputted on a single line – Krish Munot Jul 24 '16 at 16:27
11

You can also read from sys.stdin

import sys

a,b = map(int,sys.stdin.readline().split())
MAK
  • 26,140
  • 11
  • 55
  • 86
6

I am new at this stuff as well. Did a bit of research from the python.org website and a bit of hacking to get this to work. The raw_input function is back again, changed from input. This is what I came up with:

i,j = raw_input("Enter two values:  ").split()
i = int(i)
j = int(j)

Granted, the code is not as elegant as the one-liners using C's scanf or C++'s cin. The Python code looks closer to Java (which employs an entirely different mechanism from C, C++ or Python) such that each variable needs to be dealt with separately.

In Python, the raw_input function gets characters off the console and concatenates them into a single str as its output. When just one variable is found on the left-hand-side of the assignment operator, the split function breaks this str into a list of str values .

In our case, one where we expect two variables, we can get values into them using a comma-separated list for their identifiers. str values then get assigned into the variables listed. If we want to do arithmetic with these values, we need to convert them into the numeric int (or float) data type using Python's built-in int or float function.

I know this posting is a reply to a very old posting and probably the knowledge has been out there as "common knowledge" for some time. However, I would have appreciated a posting such as this one rather than my having to spend a few hours of searching and hacking until I came up with what I felt was the most elegant solution that can be presented in a CS1 classroom.

Max
  • 59
  • 6
1

Firstly read the complete line into a string like

    string = raw_input()

Then use a for loop like this

    prev = 0 
    lst = []
    index = 0 
    for letter in string :
        if item == ' ' or item == '\n' :
            lst.append(int(string[prev:index])
            prev = index + 1

This loop takes a full line as input to the string and processes the parts in it individually and then appends the numbers to the list - lst after converting them to integers .

Pinkoo
  • 127
  • 7
1

you can read 2 int values by using this in python 3.6.1

n,m = map(int,input().strip().split(" "))
saigopi.me
  • 14,011
  • 2
  • 83
  • 54
  • 2
    Can you add an explanation of your code and why it solves the issue? *Code only* answers are often not that helpful. – Zabuzard Aug 28 '17 at 17:32
1

You can also use this method for any number of inputs. Consider the following for three inputs separated by whitespace:

import sys

S = sys.stdin.read()
S = S.split()
S = [int(i) for i in S]
l = S[0]
r = S[1]
k = S[2]
Bart Van Loon
  • 1,430
  • 8
  • 18
0

or you can do this

input_user=map(int,raw_input().strip().split(" "))
Tunaki
  • 132,869
  • 46
  • 340
  • 423
deepakkumar
  • 147
  • 2
  • 7
0

You can use this method for taking inputs in one line

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

Keep in mind you can any number of variables in the LHS of this statement.

To take the inputs as string, use str instead of int

And to take list as input

a = list(map(int,input.split()))
PeakyBlinder
  • 1,059
  • 1
  • 14
  • 35
0

in python 3.9 , use a, b = map(int,input().split()) because you will get raw_input() not defined