How to take space-separated integer input in Python 3 | Example code

Use input(), map() and split() function to take space-separated integer input in Python 3. You have to use list() to convert the map to a list.

list(map(int,input().split())) 

Where:

  • input() accepts a string from STDIN.
  • split() splits the string about whitespace character and returns a list of strings.
  • map() passes each element of the 2nd argument to the first argument and returns a map object

Example take space-separated integer input in Python 3

Simple example code stage user multiple integers input, each separated space.

print("Enter the numbers: ")

inp = list(map(int, input().split()))

print(inp)

Output:

How to take space-separated integer input in Python 3

Do comment if you have any doubts and suggestions on this Python input program.

Note: IDE: PyCharm 2021.1.3 (Community Edition)

Windows 10

Python 3.7

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


Leave a Reply

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