This is not a challenge. I'm wondering if it's at all possible to get user input into two separate variables in python (2 or 3) with less than 19 bytes. These are all the shortest I can get:

a=input();b=input() (newline replaced with semicolon for readability)

Is there any shorter way to do this?

  • 2
    \$\begingroup\$ The third one would probably be the best if you are taking in more than two inputs. \$\endgroup\$ – SuperJedi224 May 27 '15 at 17:26
  • 1
    \$\begingroup\$ I was going to say command line arguments with a,b=sys.argv[1:2] or whatever but then you need an import sys (right?) \$\endgroup\$ – Alex A. May 27 '15 at 17:31
  • \$\begingroup\$ In python3 a,b=input().split() would be another 19 bytes solution, not an improvement though. Note that that means a and b are strings and the input is entered in one go seperated by a space. \$\endgroup\$ – Matty May 27 '15 at 17:34
  • \$\begingroup\$ Not sure if it helps, but in some languages you could use a vector to get two numbers, rather than 2 variables. For two arbitrary variables a more complicated container may be used. \$\endgroup\$ – Dennis Jaheruddin May 28 '15 at 7:56
  • \$\begingroup\$ a,b=input() in Py2 \$\endgroup\$ – Oliver Ni Nov 23 '16 at 16:35

If prompting the user doesn't matter, this is slightly shorter than eval for 3 or more:


It's also shorter even if prompting is not ok:


Sadly, xnor's idea doesn't save anything for 2 inputs as it is still longer (19 bytes). However if you have some string (or appropriate iterable) with the appropriate length already (because magic?) then it could be shorter.

xnor found an example of a magic string for 3 inputs in Python 2 only:


This also requires that the prompts not be important, though.

This trick actually helps to save bytes for large numbers of inputs by using 1eX notation (getting x+3 inputs):



Python's map function performs the function it is passed on each element of the iterable that is also passed to it, and returns the list (or map object) containing the results of these calls. For the first example, the map could be decomposed to:

[input(1), input(1), input(1)]
  • 2
    \$\begingroup\$ Doing map(input,'xxx') is equal-length for 3 vars, better for 2, and worse for 4+. \$\endgroup\$ – xnor May 27 '15 at 20:42
  • 3
    \$\begingroup\$ If, by some chance, you need to take exactly 22 inputs, map(input,`id`) is shorter :-). Similarly, map(input,`.1`) for 19 inputs. \$\endgroup\$ – xnor May 27 '15 at 21:15
  • 3
    \$\begingroup\$ Actually, a,b,c=map(input,`.5`) does save a char for 3 inputs, since .5 is represented as 0.5. \$\endgroup\$ – xnor May 27 '15 at 21:19
  • \$\begingroup\$ Yeah, that's good. Cool trick! \$\endgroup\$ – James May 28 '15 at 17:26

For 3 or more inputs, this is shorter:


Note that unless the question specifies the input type or requires a complete program, you can take input as function arguments, cutting out the need for input(). Where this is not an option, the following may help in certain cases.

If you want single characters in the variables a and b, you can use


Entering a 2 character string will then result in the first character being assigned to a and the second character being assigned to b. (Less than 2 or more than 2 characters in the string will give an error.)

If you require more than a single character per variable then this approach will not work. This could still be used to enter 2 single digit numbers as a string. If numbers beyond 9 are required then the full range of potential input characters could be interpreted as numbers.

Note that this also works for more than 2 variables:


This works because in Python a string is a sequence of characters, and can be used anywhere a sequence is expected.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.