Taking multiple user Input values in one line using Python

avatar

Borislav Hadzhiev

Last updated: Aug 24, 2022

banner

Photo from Unsplash

Taking multiple user Input values in one line using Python #

To take multiple user input values in one line:

  1. Use the input() function to take multiple, space-separated values.
  2. Use the str.split() function to split the values into a list.
  3. Convert the strings in the list to integers if taking numeric input values.
main.py
# ✅ Taking multiple string user input values in one line my_list = input('Enter space-separated words: ').split() print(my_list) # -------------------------------------------------------- # ✅ Taking multiple integer user input values in one line my_list = input('Enter space-separated integers: ').split() my_list = [int(item) for item in my_list] print(my_list)

take multiple string input values in 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 code snippet takes multiple string values from user input in one line.

main.py
my_list = input('Enter space-separated words: ').split() 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('a b c'.split()) # 👉️ ['a', 'b', 'c']

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 comma-separated words: ').split(',') print(my_list)

take multiple input values in one line comma separator

If you need to take multiple integer values from user input in one line, use the int() class to convert each value to an integer.

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

take multiple integer input values in one line

We used a list comprehension to iterate over the list of string input values.

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.

Alternatively, you can use the map() function.

To take multiple user input values in one line:

  1. Use the input() function to take multiple, space-separated values.
  2. Use the map() function to convert each input value to an integer if taking numeric inputs.
  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) # 👉️ [1, 2, 3, 4, 5] print(my_list[:3]) # 👉️ [1, 2, 3]

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.