Alex Rivera | Logout

Read two variables in a single line with Python

Asked 2009-10-19T11:00:21.233
16

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..

Edit
Report

1 Answer

11

You can also read from sys.stdin

import sys

a,b = map(int,sys.stdin.readline().split())
answered 2009-10-19T18:04:29.417

Your Answer