How to create a list with the characters of a string? [duplicate]
Is it possible to transform a string into a list, like this:
"5+6"
into
["5", "+", "6"]
list('5+6')
returns
['5', '+', '6']
Yes, very simply:
>>> s = "5+6"
>>> list(s)
['5', '+', '6']
Using map inbuilt list creation to work
Code:
map(None,"sart")
Output:
['s', 'a', 'r', 't']
You can also use list comprehension like:
lst = [x for x in "5+6"]
print(lst)
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
精彩评论