Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 421
user1352521 Avatar asked Sep 07 '25 17:09

user1352521


2 Answers

>>> 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'
like image 144
Paolo Bergantino Avatar answered Sep 09 '25 19:09

Paolo Bergantino


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'
like image 40
garnertb Avatar answered Sep 09 '25 21:09

garnertb