I'm wondering how to take user input and make a list of every character in it.
magicInput = input('Type here: ')
And say you entered "python rocks" I want a to make it a list something like this
magicList = [p,y,t,h,o,n, ,r,o,c,k,s]
But if I do this:
magicInput = input('Type here: ')
magicList = [magicInput]
The magicList is just
['python rocks']
Use the built-in list()
function:
magicInput = input('Type here: ')
magicList = list(magicInput)
print(magicList)
Output
['p', 'y', 't', 'h', 'o', 'n', ' ', 'r', 'o', 'c', 'k', 's']
gtlamber is right. But you don't need actualy to do anyting as the string has most of the list interface (means that you can treat string as a list). You can do for instance:
print(magicInput[1])
print(magicInput[2:4])
Output:
'y'
'th'
Another simple way would be to traverse the input and construct a list taking each letter
magicInput = input('Type here: ')
list_magicInput = []
for letter in magicInput:
list_magicInput.append(letter)
or you can simply do
x=list(input('Thats the input: ')
and it converts the thing you typed it as a list
a=list(input()).
它将输入转换为列表,就像我们要将输入转换为整数一样。
a=(int(input())) #typecasts input to int
using list comprehension,
x = [_ for _ in input()]
print(x)
produces
python rocks
['p', 'y', 't', 'h', 'o', 'n', ' ', 'r', 'o', 'c', 'k', 's']
[Program finished]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.