Using Map input split to get multiple input values from user in one line in Python. Here is code will query the user for input, and then split it into words, convert these words into integers, and unpack it into two variables x and y.
x, y = map(int, input().split())
It works as follows:
input()will query the user for input, and read one line of user input;
.split()will split that input into a list of “words”;
map(int, ...)will call
inton each word, it will to that lazily (although that is not important here); and
x, y = ...will unpack the expression into two elements, and assign the first one to
nand the second one to
Example Map input split in Python
Simple example code reads two numbers from input and typecasts them to int using the map function in Python.
x, y = map(int, input("Enter 2 number with space: ").split()) print("First Number: ", x) print("Second Number: ", y)
Do comment if you have any doubts or suggestions on this Python input code.
Note: IDE: PyCharm 2021.3 (Community Edition)
All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.
Degree in Computer Science and Engineer: App Developer and has multiple Programming languages experience. Enthusiasm for technology & like learning technical.