14

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

"5+6"

into

["5", "+", "6"]
vaultah
  • 40,483
  • 12
  • 109
  • 137
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
  • 194,053
  • 32
  • 286
  • 259
3

Yes, very simply:

>>> s = "5+6"
>>> list(s)
['5', '+', '6']
bgporter
  • 33,237
  • 8
  • 58
  • 65
1

Using map inbuilt list creation to work

Code:

map(None,"sart")

Output:

['s', 'a', 'r', 't']
The6thSense
  • 7,631
  • 6
  • 30
  • 63
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,156
  • 10
  • 14