Skip to content

Input split Python | Example code

  • by

If you want to split input by space or any other splitter then just use the split method with the input function in Python.

split slits a string by a space by default, but you can change this behavior:

input().split(separator, maxsplit)
  • separator (optional): The delimiter on which the string will be split. If not provided, the method will split the string at any whitespace characters (e.g., spaces, tabs, newlines).
  • maxsplit (optional): Specifies the maximum number of splits to be done. If not provided, there is no limit on the number of splits.

Example Input split Python

Simple example code split input by space.

res = input("Write:").split(" ")



Input split Python


input("Please enter two digits separated by space").split()

Note: that .split(" ") isn’t needed as that is what it is by default.

Take 2 integer values

x, y = map(int, input().split())  # you can change the int to specify or intialize any other data structures



1 2

Splitting a string using a specific separator

numbers = "1-2-3-4-5"
num_list = numbers.split('-')
# Output: ['1', '2', '3', '4', '5']

Using maxsplit to limit the number of splits

text = "apple,banana,orange,grape"
fruits = text.split(',', 2)  # Split at the first two occurrences of ","
# Output: ['apple', 'banana', 'orange,grape']

In these examples, the split() method is applied to a string, and the resulting substrings are stored in a list. It’s important to note that the original string remains unchanged, and the method returns a new list containing the split substrings.

How to get user input of a list in Python?

To get user input of a list in Python, you can use the input() function to take input from the user and then convert it into a list using appropriate methods. Here’s a step-by-step guide:

  1. Ask the user to enter the elements of the list as a comma-separated string.
  2. Use the split() method to split the input string based on the commas and create a list of individual elements.
  3. Optionally, you can convert the elements to the desired data type (e.g., integers, floats) if needed.
# number of elements 
n = int(input("Enter number of elements : "))

# Below line read inputs from user using map() function  
a = list(map(int, input("\nEnter the numbers : ").strip().split()))[:n]

print("\nList is - ", a)


Enter the number of elements: 2

Enter the numbers: 1 2

The list is – [1, 2]

Comment if you have any doubts or suggestions on this Python input program code.

Note: IDE: PyCharm 2021.3.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. Required fields are marked *