Skip to content
Advertisement

Read two variables in a single line with Python [duplicate]

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 20n"

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

Advertisement

Answer

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')

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement