27

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?

Community
  • 1
  • 1
user1352521
  • 341
  • 1
  • 5
  • 8
  • 1
    Exact duplicate of [How to create a list with the characters of a string?](http://stackoverflow.com/q/5501641/1142167) – Joel Cornett May 15 '12 at 23:26

3 Answers3

66
>>> 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'
Paolo Bergantino
  • 480,997
  • 81
  • 517
  • 436
  • 2
    Wow. I tried a bunch of elaborate techniques and it was just one command. Thank you so much. Saved me from a headache. – user1352521 May 15 '12 at 23:26
6

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'
garnertb
  • 9,454
  • 36
  • 38
1
yourstring = 'abc'
[char for char in yourstring]
cobie
  • 7,023
  • 11
  • 38
  • 60