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
Separate Alphabets and Numbers in a String - Python
The task is to separate alphabets and numbers from a string. For example, given "a1b2c3", the output should be alphabets "abc" and numbers "123".Using List ComprehensionList comprehension offers a concise and efficient way to separate alphabets and numbers from a string. It iterates through each cha
2 min read
Python - Substring concatenation by Separator
Sometimes, while working with Python Lists, we can have problem in which we need to perform the concatenation of strings in a list till the separator. This can have application in domains in which we need chunked data. Lets discuss certain ways in which this task can be performed. Method #1 : Using
5 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 - Check for spaces in string
Sometimes, while working with strings in Python, we need to determine if a string contains any spaces. This is a simple problem where we need to check for the presence of space characters within the string. Let's discuss different methods to solve this problem.Using 'in' operator'in' operator is one
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 a String by Custom Lengths
Given a String, perform split of strings on the basis of custom lengths. Input : test_str = 'geeksforgeeks', cus_lens = [4, 3, 2, 3, 1] Output : ['geek', 'sfo', 'rg', 'eek', 's'] Explanation : Strings separated by custom lengths.Input : test_str = 'geeksforgeeks', cus_lens = [10, 3] Output : ['geeks
2 min read
Python - Avoid Spaces in string length
When working with strings in Python, we may sometimes need to calculate the length of a string excluding spaces. The presence of spaces can skew the length when we're only interested in the number of non-space characters. Let's explore different methods to find the length of a string while ignoring
3 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