How do I convert user input into a list?
Question:
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']
Answers:
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']
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)
It may not be necessary to do any conversion, because the string supports many list operations. For instance:
print(magicInput[1])
print(magicInput[2:4])
Output:
y
th
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()).
It converts the input into a list just like when we want to convert the input into an integer.
a=(int(input())) #typecasts input to int
using list comprehension,
magicInput = [_ for _ in input("Enter String:")]
print('magicList = [{}]'.format(', '.join(magicInput)))
produces
Enter String:python rocks
magicList = [p, y, t, h, o, n, , r, o, c, k, s]
You can use str.join()
to concatenate strings with a specified separator.
Furthermore, in your case, str.format()
may also help.
However the apostrophes will not interfere with anything you do with the list. The apostrophes show that the elements are strings.
Method 2:
magicInput = ','.join(input('Enter String: '))
print(f'nmagicList: [{magicInput}]')
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']
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)
It may not be necessary to do any conversion, because the string supports many list operations. For instance:
print(magicInput[1])
print(magicInput[2:4])
Output:
y
th
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()).
It converts the input into a list just like when we want to convert the input into an integer.
a=(int(input())) #typecasts input to int
using list comprehension,
magicInput = [_ for _ in input("Enter String:")]
print('magicList = [{}]'.format(', '.join(magicInput)))
produces
Enter String:python rocks
magicList = [p, y, t, h, o, n, , r, o, c, k, s]
You can use str.join()
to concatenate strings with a specified separator.
Furthermore, in your case, str.format()
may also help.
However the apostrophes will not interfere with anything you do with the list. The apostrophes show that the elements are strings.
Method 2:
magicInput = ','.join(input('Enter String: '))
print(f'nmagicList: [{magicInput}]')