views:

3449

answers:

5

Is there a function in python to split a word into a list of single letters? e.g

s="Word to Split"

to get

wordlist=['W','o','r','d','','t','o' ....]

+37  A: 
>>> list("Word to Split")
['W', 'o', 'r', 'd', ' ', 't', 'o', ' ', 'S', 'p', 'l', 'i', 't']
Greg Hewgill
Any reason you know of why "Word to Split".split('') doesn't do the same thing. It doesn't, but really seems like it should.
Walter Nissen
@Walter Nissen: I get "ValueError: empty separator" when trying that. The empty regex is not terribly well defined.
Greg Hewgill
A: 

Why do you need it? Most of the time you can use a string like a list.

KovBal
You can't sort, randomize, etc a string. Don't know his specific case, but these aren't uncommon operations.
Cody Brocious
In fact, you can't modify a string in any way. That's why they are called "Immutable".
Matthew Schinckel
A: 

Try:

s = "Word to Split"
wordlist = list(s)
wordlist

That should give you what you need (['W','o','r','d',' ','t','o',' ','S','p','l','i','t']).

paxdiablo
A: 

The list function will do this

>>> list('foo')
['f', 'o', 'o']
Mez
A: 

Abuse of the rules, same result: (x for x in 'Word to split')

Actually an iterator, not a list. But it's likely you won't really care.

Tim Ottinger