0

How to take a line input in python Consider I need to take input in a line consisting integers seperated by spaces I want to give 5 6 and store them in a and b

a=input()
b=input()

If I take in above format, it takes only when I press enter

3
  • 1
    what python version? Commented Aug 14, 2014 at 18:38
  • The input function "… reads a line from input …", which is why you have to hit enter after for each value. To accept multiple values on a line, you want to just input once, then parse the line (which is easy, as @saliesh shows). Commented Aug 14, 2014 at 18:52
  • I've tagged this with python-3.x, because the accepted answer is wrong for 2.x. If you're on 2.x, please edit to change the tag, and leave @saliesh a comment so he can change his answer appropriately (to use raw_input, and to explain why). Commented Aug 14, 2014 at 18:56

1 Answer 1

1

Its very simple

j,k=input().split(" ")
j=int(j)
k=int(k)
Sign up to request clarification or add additional context in comments.

4 Comments

input().split() does the same thing you don't need " "
As a one-line solution j,k = map(int, input().split())
@PadraicCunningham: In fact, it's probably better to use split(); if the user types 3 4 with two spaces, it will probably be surprising to get a ValueError: too many tuple values to unpack (expected 2)
@PadraicCunningham and sailesh thank you for all your reply's , Padric method seems to be good

Your Answer

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.