I am making a program that read 2 or more input at the same time. How can I read input that has the form a b? For example: 3 4. I need to read 2 variables: a = 3 and b = 4.
arshajii
130k26 gold badges246 silver badges293 bronze badges
-
1What Python version are you using? 3.x or 2.x?arshajii– arshajii2013年08月29日 02:11:30 +00:00Commented Aug 29, 2013 at 2:11
-
13.x :) I just started learning Python. Can you tell me some books which you think are useful for absolute beginners?windoanvn– windoanvn2013年08月29日 02:23:41 +00:00Commented Aug 29, 2013 at 2:23
1 Answer 1
You can do something like
a,b = input().split()
For example:
>>> a,b = input().split()
3 4
>>> a
'3'
>>> b
'4'
For reference, see input() and str.split().
If you want a and b to be ints, you can call map() (as is described in the comments):
a,b = map(int, input().split())
answered Aug 29, 2013 at 1:59
arshajii
130k26 gold badges246 silver badges293 bronze badges
Sign up to request clarification or add additional context in comments.
3 Comments
icktoofay
And if they needed to be
ints, for example, you could wrap a map with an appropriate first argument around input().split().inspectorG4dget
@icktoofay: (mentioning this just for completeness) or you could do a list comprehension:
[int(i) for i in input().split()]arshajii
@user2727512 As the comments describe,
a,b = map(int, input().split()) would work.lang-py