Split an input from user in Python

avatar

Borislav Hadzhiev

Last updated: Jun 25, 2022

banner

Photo from Unsplash

Split an input from user in Python #

Use the str.split() method to split an input from the user, e.g. user_input.split(' '). The str.split method will split the string on the provided delimiter and will return a list containing the results.

main.py
from_user = input('Enter two words: ') print(from_user) # 👉️ 'one two' # 👇️ split with space separator result = from_user.split(' ') print(result) # 👉️ ['one', 'two'] first, second = result print(first) # 👉️ one print(second) # 👉️ two

We used the str.split() method to split user input.

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

The method takes the following 2 parameters:

NameDescription
separatorSplit the string into substrings on each occurrence of the separator
maxsplitAt most maxsplit splits are done (optional)

If the separator is not found in the string, a list containing only 1 element is returned.

If you need to take numbers from the user, make sure to convert them to integers or floating-point numbers.

main.py
from_user = input('Enter two numbers: ') print(from_user) # 👉️ '5 10' # 👇️ split with space separator result = [int(x) for x in from_user.split(' ')] print(result) # 👉️ [5, 10] first, second = result print(first) # 👉️ 5 print(second) # 👉️ 10

The input() function is always guaranteed to return a string, even if the user enters a number.

We can convert the string to a number by using the int() or float() classes.

The example splits the string on each space and uses a list comprehension to iterate over the list and to convert each substring to an integer.

List comprehensions are used to perform some operation for every element, or select a subset of elements that meet a condition.

When the str.split() method is called without a separator, it considers consecutive whitespace characters as a single separator.

This can be useful to handle a scenario where the user enters multiple spaces between the words or digits.

main.py
from_user = input('Enter two numbers: ') print(from_user) # 👉️ '5 10' 👈️ has multiple spaces between digits # 👇️ split with space separator result = [int(x) for x in from_user.split()] print(result) # 👉️ [5, 10] first, second = result print(first) # 👉️ 5 print(second) # 👉️ 10

This approach also handlers the scenario where the string starts with or ends with a space.

You can also use any other separator when splitting input from the user, it doesn't have to be a space.

main.py
from_user = input('Enter two numbers: ') print(from_user) # 👉️ '5,10' # 👇️ split with space separator result = [int(x) for x in list(filter(None, from_user.split(',')))] print(result) # 👉️ [5, 10]

The example splits the input string on each occurrence of a comma.

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.