Python program to input a comma separated string
Given an input string that is comma-separated instead of space. The task is to store this input string in a list or variables. This can be achieved in Python using two ways:
- Using List comprehension and
split() - Using
map()andsplit()
Method 1: Using List comprehension and split()
split() function helps in getting multiple inputs from the user. It breaks the given input by the specified separator. If the separator is not provided then any white space is used as a separator. Generally, users use a split() method to split a Python string but one can also use it in taking multiple inputs.
Example:
# Python program to take a comma # separated string as input # Taking input when the numbers # of input are known and storing # in different variables # Taking 2 inputs a, b = [int(x) for x in input("Enter two values\n").split(', ')] print("\nThe value of a is {} and b is {}".format(a, b)) # Taking 3 inputs a, b, c = [int(x) for x in input("Enter three values\n").split(', ')] print("\nThe value of a is {}, b is {} and c is {}".format(a, b, c)) # Taking multiple inputs L = [int(x) for x in input("Enter multiple values\n").split(', ')] print("\nThe values of input are", L) |
Output:
Enter two values 1, 2 The value of a is 1 and b is 2 Enter three values 1, 2, 3 The value of a is 1, b is 2 and c is 3 Enter multiple values 1, 22, 34, 6, 88, 2 The values of input are [1, 22, 34, 6, 88, 2]
Method 2: Using map() and split()
map() function returns a list of the results after applying the given function to each item of a given iterable (list, tuple etc.)
# Python program to take a comma # separated string as input # Taking input when the numbers # of input are known and storing # in different variables # Taking 2 inputs a, b = map(int, input("Enter two values\n").split(', ')) print("\nThe value of a is {} and b is {}".format(a, b)) # Taking 3 inputs a, b, c = map(int, input("Enter three values\n").split(', ')) print("\nThe value of a is {}, b is {} and c is {}".format(a, b, c)) # Taking multiple inputs L = list(map(int, input("Enter multiple values\n").split(', '))) print("\nThe values of input are", L) |
Output:
Enter two values 1, 2 The value of a is 1 and b is 2 Enter three values 1, 2, 3 The value of a is 1, b is 2 and c is 3 Enter multiple values 1, 2, 3, 4 The values of input are [1, 2, 3, 4]
Recommended Posts:
- Python | Convert key-value pair comma separated string into dictionary
- Python - Custom Split Comma Separated Words
- Python | Convert list of strings to space separated string
- Python | Add comma between numbers
- Python - Consecutive Pairs comma removal
- Python | Find all close matches of input string from a list
- Generate two output strings depending upon occurrence of character in input string in Python
- Python program to sort a string
- Python program to check if a string is palindrome or not
- Python program to split and join a string
- Python program to create a dictionary from a string
- Python | Program to convert String to a List
- Python program to check if a given string is Keyword or not
- Python | Program to convert a tuple to a string
- Python program to check if given string is pangram
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

