Skip to content

Map input split Python | Example code

  • by

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:

  1. input() will query the user for input, and read one line of user input;
  2. .split() will split that input into a list of “words”;
  3. map(int, ...) will call int on each word, it will to that lazily (although that is not important here); and
  4. x, y = ... will unpack the expression into two elements, and assign the first one to n and the second one to S.

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)

Output:

Map input split Python

Do comment if you have any doubts or suggestions on this Python input code.

Note: IDE: PyCharm 2021.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.