Input a comma separated string - Python
Last Updated :
13 May, 2025
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.
Related articles
Similar Reads
Python - Custom Split Comma Separated Words While working with Python, we can have problem in which we need to perform the task of splitting the words of string on spaces. But sometimes, we can have comma separated words, which have comma's joined to words and require to split them separately. Lets discuss certain ways in which this task can
5 min read
Convert List to Delimiter Separated String - Python The task of converting a list to a delimiter-separated string in Python involves iterating through the list and joining its elements using a specified delimiter. For example, given a list a = [7, "Gfg", 8, "is", "best", 9] and a delimiter "*", the goal is to produce a single string where each elemen
3 min read
Split and join a string in Python The goal here is to split a string into smaller parts based on a delimiter and then join those parts back together with a different delimiter. For example, given the string "Hello, how are you?", you might want to split it by spaces to get a list of individual words and then join them back together
3 min read
Python | Exceptional Split in String Sometimes, while working with Strings, we may need to perform the split operation. The straightforward split is easy. But sometimes, we may have a problem in which we need to perform split on certain characters but have exceptions. This discusses split on comma, with the exception that comma should
4 min read
Python - Split String on all punctuations Given a String, Split the String on all the punctuations. Input : test_str = 'geeksforgeeks! is-best' Output : ['geeksforgeeks', '!', 'is', '-', 'best'] Explanation : Splits on '!' and '-'. Input : test_str = 'geek-sfo, rgeeks! is-best' Output : ['geek', '-', 'sfo', ', ', 'rgeeks', '!', 'is', '-', '
3 min read
Split a String by a Delimiter in Python In Python Programming, the split() method is used to split a string into a list of substrings which is based on the specified delimiter. This method takes the delimiter as an argument and returns the list of substrings. Along with this method, we can use various approaches wot split a string by the
2 min read
Convert key-value pair comma separated string into dictionary - Python In Python, we might have a string containing key-value pairs separated by commas, where the key and value are separated by a colon (e.g., "a:1,b:2,c:3"). The task is to convert this string into a dictionary where each key-value pair is represented properly. Let's explore different ways to achieve th
3 min read
Python | Convert String ranges to list Sometimes, while working in applications we can have a problem in which we are given a naive string that provides ranges separated by a hyphen and other numbers separated by commas. This problem can occur across many places. Let's discuss certain ways in which this problem can be solved. Method #1:
6 min read
Join List With Separator in Python The task of joining a list with a separator in Python involves combining the elements of the list into a single string, where each element is separated by a specified separator. The separator, such as a comma, can be placed between the elements to create a well-formatted string. For example, given t
3 min read
Python - Convert delimiter separated Mixed String to valid List Given a string with elements and delimiters, split elements on delimiter to extract with elements ( including containers). Input : test_str = "6*2*9*[3, 5, 6]*(7, 8)*8*4*10", delim = "*" Output : [6, 2, 9, [3, 5, 6], (7, 8), 8, 4, 10] Explanation : Containers and elements separated using *. Input :
10 min read