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
Method 1: Using List comprehension and
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.
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() function returns a list of the results after applying the given function to each item of a given iterable (list, tuple etc.)
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]
- 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 - Convert List to delimiter separated String
- Python | Add comma between numbers
- Python - Consecutive Pairs comma removal
- Python - Convert Delimiter separated list to Number
- Python - Construct dictionary Key-Value pairs separated by delimiter
- Python - Sort words separated by Delimiter
- Generate two output strings depending upon occurrence of character in input string in Python
- Python | Find all close matches of input string from a list
- How to input multiple values from user in one line in Python?
- Python Input Methods for Competitive Programming
- Vulnerability in input() function – Python 2.x
- Taking input from console in Python
- Taking input in Python
- Python | Get a list as input from user
- Take Matrix input from user in Python
- Python regex | Check whether the input is Floating point number or not
- Python | Accepting Script Input
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.