Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python- Turning user input into a list

Tags:

python

input

Is there a way to ask for user input and turn their input into a list, tuple, or string for that matter? I want a series of numbers to insert into a matrix. I could tell them to type all the numbers into the console with no spaces and iterate through them but are there any other ways to do this?

like image 609
Joseph hooper Avatar asked Mar 31 '15 00:03

Joseph hooper


People also ask

How do you take a list of inputs in one line in Python?

To take list input in Python in a single line use input() function and split() function. Where input() function accepts a string, integer, and character input from a user and split() function to split an input string by space.

How do you convert input to number in Python?

To convert, or cast, a string to an integer in Python, you use the int() built-in function. The function takes in as a parameter the initial string you want to convert, and returns the integer equivalent of the value you passed.

What does input () split () do?

a) split () This function is generally used to separate a given string into several substrings. However, you can also use it for taking multiple inputs. The function generally breaks the given input by the specified separator and in case the separator is not provided then any white space is considered as a separator.


1 Answers

You can simply do as follows:

user_input = input("Please provide list of numbers separated by comma, e.g. 1,2,3: ")

a_list =  list(map(float,user_input.split(',')))
print(a_list)
# example result: [1, 2, 3]
like image 134
Marcin Avatar answered Oct 07 '22 19:10

Marcin