1

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
asked Aug 29, 2013 at 1:58
2
  • 1
    What Python version are you using? 3.x or 2.x? Commented Aug 29, 2013 at 2:11
  • 1
    3.x :) I just started learning Python. Can you tell me some books which you think are useful for absolute beginners? Commented Aug 29, 2013 at 2:23

1 Answer 1

3

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
Sign up to request clarification or add additional context in comments.

3 Comments

And if they needed to be ints, for example, you could wrap a map with an appropriate first argument around input().split().
@icktoofay: (mentioning this just for completeness) or you could do a list comprehension: [int(i) for i in input().split()]
@user2727512 As the comments describe, a,b = map(int, input().split()) would work.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.