Python: Taking multiple integers from user input in one line

avatar

Borislav Hadzhiev

Last updated: Aug 26, 2022

banner

Photo from Unsplash

Python: Taking multiple integers from user input in one line #

To take multiple integers from user input in one line:

  1. Use the input() function to take multiple, space-separated integers.
  2. Use the str.split() function to split the string into a list.
  3. Use the int() class to convert each string in the list to an integer.
main.py
# ✅ taking multiple space-separated integers from user input my_list = input('Enter space-separated integers: ').split() my_list = [int(item) for item in my_list] print(my_list) # -------------------------------------------------------- # ✅ taking multiple comma-separated integers from user input my_list = input('Enter comma-separated integers: ').split(',') my_list = [int(item) for item in my_list] print(my_list)

take multiple integers input one line

We used the input() function to take user input.

The input function takes an optional prompt argument and writes it to standard output without a trailing newline.

The function then reads the line from input, converts it to a string and returns the result.

Note that the input() function is guaranteed to return a string, even if the user enters an integer.

The first example takes multiple, space-separated integers from the user.

main.py
my_list = input('Enter space-separated integers: ').split() my_list = [int(item) for item in my_list] print(my_list)

The str.split() method splits the string into a list of substrings using a delimiter.

When no separator is passed to the str.split() method, it splits the input string on one or more whitespace characters.
main.py
print('1 3 7'.split()) # 👉️ ['1', '3', '7']

The last step is to use a list comprehension to convert the strings in the list to integers.

main.py
my_list = input('Enter space-separated integers: ').split() my_list = [int(item) for item in my_list] print(my_list)
List comprehensions are used to perform some operation for every element or select a subset of elements that meet a condition.

On each iteration, we pass the current list item to the int() class to convert it to an integer.

You can pass a separator string to the split() method if you want to split the input string on another character, e.g. a comma.

main.py
my_list = input('Enter space-separated integers: ').split(',') my_list = [int(item) for item in my_list] print(my_list)

multiple integers user input comma separator

Alternatively, you can use the map() function.

To take multiple integers from user input in one line:

  1. Use the input() function to take multiple, space-separated integers.
  2. Use the map() function to convert each string to an integer.
  3. Use the list() class to convert the map object to a list.
main.py
my_list = list( map( int, input('Enter space-separated integers: ').split() ) ) print(my_list)

We used the str.split() function to split the string of multiple, space-separated integers.

The map() function takes a function and an iterable as arguments and calls the function with each item of the iterable.

The map function calls the int() class with each value in the list and converts each string to an integer.

The last step is to use the list() class to convert the map object to a list.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.