Is there a function in python to split a word into a list?

Is there a function in python to split a word into a list of individual letters? eg:

s="Word to Split" 

To obtain

 wordlist=['W','o','r','d','','t','o' ....] 
+85
function python split
Sep 22 '08 at 7:40
source share
7 answers
 >>> list("Word to Split") ['W', 'o', 'r', 'd', ' ', 't', 'o', ' ', 'S', 'p', 'l', 'i', 't'] 
+195
Sep 22 '08 at 7:42
source share

The easiest way is to simply use list() , but there is at least one more option:

 s = "Word to Split" wordlist = list(s) # option 1, wordlist = [ch for ch in s] # option 2, list comprehension. 

They should give you what you need:

 ['W','o','r','d',' ','t','o',' ','S','p','l','i','t'] 

As indicated, the first is most likely the most preferable for your example, but there are use cases that can make the latter very convenient for more complex things, for example, if you want to apply some arbitrary function to the elements, for example, using

 [doSomethingWith(ch) for ch in s] 
+14
Sep 22 '08 at 7:46
source share

Violation of the rules, the same result: (x for x in "Word for sharing")

This is actually an iterator, not a list. But most likely you do not care.

+4
Sep 22 '08 at 14:36
source share

The list function will do this.

 >>> list('foo') ['f', 'o', 'o'] 
+2
Sep 22 '08 at 7:47
source share

Here is just a one line solution

 >>> mystring = "This is my string" >>> list(mystring) ['T', 'h', 'i', 's', ' ', 'i', 's', ' ', 'm', 'y', ' ', 's', 't', 'r', 'i', 'n', 'g'] 

You can see that even white spaces are also converted to an item in the list

0
Jun 15 '17 at 6:51
source share
 text = "just trying out" word_list = [] for i in range(0, len(text)): word_list.append(text[i]) i+=1 print(word_list) ['j', 'u', 's', 't', ' ', 't', 'r', 'y', 'i', 'n', 'g', ' ', 'o', 'u', 't'] 
0
Mar 11 '19 at 10:58
source share

Convert string to list:

 myword = 'code to split given string' list(myword) 
-one
Apr 04 '19 at 5:13
source share



All Articles