How do I convert string characters into a list? [duplicate]

匿名 (未验证) 提交于 2019-12-03 02:56:01

问题:

Possible Duplicate:
How to create a list with the characters of a string?

Example:

'abc' 

becomes

['a', 'b', 'c'] 

Is it a combination of split and slicing?

回答1:

>>> x = 'abc' >>> list(x) ['a', 'b', 'c'] 

Not sure what you are trying to do, but you can access individual characters from a string itself:

>>> x = 'abc' >>> x[1] 'b' 


回答2:

If you need to iterate over the string you do not even need to convert it to a list:

>>> n = 'abc' >>> for i in n: ...     print i ...  a b c 

or

>>> n[1] 'b' 


回答3:

yourstring = 'abc' [char for char in yourstring] 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!