This does different things on Python 2 and Python 3.
Python 3's input()
is Python 2's raw_input()
, and will always return a string.
When you do a tuple unpacking as in a, b, = (1, 2)
, the amount of elements on the right must match the amount of names on the left. If they don't, you'll get a ValueError
. As strings are iterable, a, b = input()
will work, if the user enters a two character long string. Any other string will crash your program.
To ask your user for more than one input at once, clearly define the format in the prompt, e.g., inp = input('Please input your first and last name, separated by a comma: ')
, then parse the input afterwards: first_name, last_name = inp.split(',')
.
Note that this will still crash your program if they enter an incorrect string, with more or less than one comma, but that's reasonably simple to check for, notify the user, and try again.
On Python 2, input()
attempts to coerce the value to a natural Python value, so if the user inputs [1, 2]
, input()
will return a Python list. This is a bad thing, as you will still need to validate and sanitise the user data yourself, and you may have wanted "[1, 2]"
instead.