Input a comma separated string - Python

Last Updated : 13 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Handling comma-separated input in Python involves taking user input like '1, 2, 3' and converting it into usable data types such as integers or floats. This is especially useful when dealing with multiple values entered in a single line. Let's explore different efficient methods to achieve this:

Using list comprehension

List comprehension allows you to read and process a comma-separated string in one line. It splits the string, converts each value e.g., to int and stores them in a list. It's clean, readable and ideal for working with lists directly.

Python
# Taking 2 inputs
a, b = [int(x) for x in input("Enter two values\n").split(', ')]
print(a, b)

# Taking multiple inputs
a = [int(x) for x in input("Enter multiple values\n").split(', ')]
print(a)

Output

Enter two values
4,7
4 7
Enter multiple values
5,7,9,9
[5, 7, 9, 9]

Explanation: List comprehension reads a comma-separated string like "4, 7", splits it using .split(', '), and converts each part to an integer in one line.

Using map()

map() applies a function like int to every item in a comma-separated string. It’s efficient and faster for type conversion, especially with large inputs. You can convert the result to a list or unpack it into variables.

Python
# Taking 2 inputs
a, b = map(int, input("Enter two values\n").split(', '))
print(a, b)

# Taking multiple inputs
a = list(map(int, input("Enter multiple values\n").split(', ')))
print(a)

Output

Enter two values
4, 4
4 4
Enter multiple values
4, 2, 5, 7
[4, 2, 5, 7]

Explanation: map() applies a function like int to all parts of a split string. It’s efficient for type conversion. You can directly unpack two inputs (a, b = map(...)) or use list(map(...)) for multiple values.

Using tuple()

This method creates a tuple from comma-separated values using map(). Useful when the data should remain fixed or immutable after input.

Python
# Taking 2 inputs
a, b = tuple(map(int, input("Enter two values\n").split(', ')))
print(a, b)

# Taking multiple inputs
a = tuple(map(int, input("Enter multiple values\n").split(', ')))
print(a)

Output

Enter two values
4, 6
4 6
Enter multiple values
4, 7, 8 , 3
(4, 7, 8, 3)

Explanation: tuple(map(...)) convert a comma-separated string into a tuple of integers. It's similar to map() with a list, but returns immutable data. Great when input values shouldn’t be changed after reading.

Using eval()

eval() treats the input as a Python expression, so entering 2, 3 returns a tuple. It’s flexible and can directly accept numeric or mixed types.

Python
# Taking 2 inputs
a, b = eval(input("Enter two values\n"))
print(a, b)

# Taking multiple inputs
a = eval(input("Enter multiple values\n"))
print(a)

Output

Enter two values
7,8
7 8
Enter multiple values
4,7,3,7
(4, 7, 3, 7)

Explanation: eval() interprets the input string as Python code. Typing 4, 5 returns a tuple (4, 5) automatically.


Next Article
Practice Tags :

Similar Reads