14

Is it possible to transform a string into a list, like this:

"5+6"

into

["5", "+", "6"]
vaultah
  • 44,105
  • 12
  • 114
  • 143
user678766
  • 171
  • 1
  • 1
  • 4
  • 5
    Do you simply want to turn single characters into a list, or do you want to tokenize the input, i.e. turn `57+23` into `["57", "+", "23"]`? – Håvard Mar 31 '11 at 14:44

5 Answers5

21
list('5+6')

returns

['5', '+', '6']
eumiro
  • 207,213
  • 34
  • 299
  • 261
3

Yes, very simply:

>>> s = "5+6"
>>> list(s)
['5', '+', '6']
bgporter
  • 35,114
  • 8
  • 59
  • 65
1

Using map inbuilt list creation to work

Code:

map(None,"sart")

Output:

['s', 'a', 'r', 't']
The6thSense
  • 8,103
  • 8
  • 31
  • 65
1

You can also use list comprehension like:

lst = [x for x in "5+6"]
print(lst)
iboarici
  • 11
  • 2
0

in python 3 you could make this ...

>>> s = 'bioinform'
>>> s
'bioinform'
>>> w = list(s)
>>> w
['b', 'i', 'o', 'i', 'n', 'f', 'o', 'r', 'm']
>>> 

but if you give list any value will give you an error so you should restart your IDLE

Mina Samir
  • 1,527
  • 14
  • 15