0
How do I make a list from a user's input in python?
4 odpowiedzi
+ 6
l=list(input())
print(l)
If I enter "hello world"
Output Will be:-
['h', 'e', 'l', 'l', 'o', 'w', 'o', 'r', 'l', 'd']
But if you want to separate them as words (not as letters) you can use split() function.
s=input().split()
print(s)
Output will be:-
['hello','world']
Hope it helps ◉‿◉
+ 4
Easiest way could using eval function. You would get the list just like you typed it:
eval(input())
Eval function can be discouraged to use, because it could give client ability to change the program during the runtime.
Another way could be using str.split method, which uses a substring to slice another string.
The slices will appear at the positions, where the substrings appear in the string and the slices will be put in a list:
", ".split(input())
If the input was: 1, B, c
You would get: ["1", "B", "c"]
The input was sliced where the substring ", " appeared.
+ 1
There's so many ways.
Easiest is the map function:
list_input = list(map(int, input().split()))
Note: Replace int with str if you expect string input and split(',') if the user enters something like, 1,2,3.