开发者

How to create a list with the characters of a string? [duplicate]

This question already has answers here: How do I split a string into a list of characters? (15 answers) Closed 2 y开发者_开发知识库ears ago.

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜