How do I split a string into a list of characters? str.split
does not work.
"foobar" → ['f', 'o', 'o', 'b', 'a', 'r']
Mateen Ulhaq
22.2k16 gold badges86 silver badges127 bronze badges
asked Feb 12, 2011 at 15:14
3
Use the list
constructor:
>>> list["foobar"]
['f', 'o', 'o', 'b', 'a', 'r']
list
builds a new list using items obtained by iterating over the input iterable. A string is an iterable -- iterating over it yields a single character at
each iteration step.
Mateen Ulhaq
22.2k16 gold badges86 silver badges127 bronze badges
answered Feb 12, 2011 at 15:16
user225312user225312
121k66 gold badges167 silver badges181 bronze badges
3
You take the string and pass it to list[]
s = "mystring"
l = list[s]
print l
answered Feb 12, 2011 at 15:16
Senthil KumaranSenthil Kumaran
52.4k14 gold badges90 silver badges127 bronze badges
You can also do it in this very simple way without list[]:
>>> [c for c in "foobar"]
['f', 'o', 'o', 'b', 'a', 'r']
answered Mar 24, 2015 at 6:00
3
If you want to process your String one character at a time. you have various options.
uhello = u'Hello\u0020World'
Using List comprehension:
print[[x for x in uhello]]
Output:
['H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd']
Using map:
print[list[map[lambda c2: c2, uhello]]]
Output:
['H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd']
Calling Built in list function:
print[list[uhello]]
Output:
['H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd']
Using for loop:
for c in uhello:
print[c]
Output:
H
e
l
l
o
W
o
r
l
d
dtasev
4714 silver badges12 bronze badges
answered Jun 3, 2017 at 14:48
SidSid
6795 silver badges5 bronze badges
1
If you just need an array of chars:
arr = list[str]
If you want to split the str by a particular delimiter:
# str = "temp//temps" will will be ['temp', 'temps']
arr = str.split["//"]
answered Dec 13, 2018 at 21:31
0
I explored another two ways to accomplish this task. It may be helpful for someone.
The first one is easy:
In [25]: a = []
In [26]: s = 'foobar'
In [27]: a += s
In [28]: a
Out[28]: ['f', 'o', 'o', 'b', 'a', 'r']
And the second one use map
and lambda
function. It may be appropriate for more complex tasks:
In [36]: s = 'foobar12'
In [37]: a = map[lambda c: c, s]
In [38]: a
Out[38]: ['f', 'o', 'o', 'b', 'a', 'r', '1', '2']
For example
# isdigit, isspace or another facilities such as regexp may be used
In [40]: a = map[lambda c: c if c.isalpha[] else '', s]
In [41]: a
Out[41]: ['f', 'o', 'o', 'b', 'a', 'r', '', '']
See python docs for more methods
answered Sep 10, 2014 at 19:07
2
The task boils down to iterating over characters of the string and collecting them into a list. The most naïve solution would look like
result = []
for character in string:
result.append[character]
Of course, it can be shortened to just
result = [character for character in string]
but there still are shorter solutions that do the same thing.
list
constructor can be used to convert any iterable [iterators, lists, tuples, string etc.] to list.
>>> list['abc']
['a', 'b', 'c']
The big plus is that it works the same in both Python 2 and Python 3.
Also, starting from Python 3.5 [thanks to the awesome PEP 448] it's now possible to build a list from any iterable by unpacking it to an empty list literal:
>>> [*'abc']
['a', 'b', 'c']
This is neater, and in some cases more efficient than calling list
constructor directly.
I'd advise against using map
-based approaches, because map
does not return a list in Python 3. See
How to use filter, map, and reduce in Python 3.
answered Apr 5, 2016 at 17:24
vaultahvaultah
41.7k12 gold badges111 silver badges139 bronze badges
1
split[]
inbuilt function will only separate the value on the basis of certain condition but
in the single word, it cannot fulfill the condition. So, it can be solved with the help of list[]
. It internally calls the Array and it will store the value on the basis of an array.
Suppose,
a = "bottle"
a.split[] // will only return the word but not split the every single char.
a = "bottle"
list[a] // will separate ['b','o','t','t','l','e']
tuomastik
4,2555 gold badges33 silver badges45 bronze badges
answered Aug 18, 2018 at 6:53
Unpack them:
word = "Paralelepipedo"
print[[*word]]
answered Nov 26, 2019 at 12:47
enbermudasenbermudas
1,5013 gold badges18 silver badges41 bronze badges
To split a string s
, the easiest way is to pass it to list[]
. So,
s = 'abc'
s_l = list[s] # s_l is now ['a', 'b', 'c']
You can also use a list comprehension, which works but is not as concise as the above:
s_l = [c for c in s]
There are other ways, as well, but these should suffice. Later, if you want to recombine them, a simple call to "".join[s_l]
will return your list to all its former glory as a string...
answered Dec 17, 2020 at 6:53
Gary02127Gary02127
4,7811 gold badge23 silver badges28 bronze badges
You can use extend
method in list operations as well.
>>> list1 = []
>>> list1.extend['somestring']
>>> list1
['s', 'o', 'm', 'e', 's', 't', 'r', 'i', 'n', 'g']
Georgy
10.8k7 gold badges62 silver badges68 bronze badges
answered Sep 6, 2020 at 19:23
If you wish to read only access to the string you can use array notation directly.
Python 2.7.6 [default, Mar 22 2014, 22:59:38]
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> t = 'my string'
>>> t[1]
'y'
Could be useful for testing without using regexp. Does the string contain an ending newline?
>>> t[-1] == '\n'
False
>>> t = 'my string\n'
>>> t[-1] == '\n'
True
answered May 17, 2014 at 14:28
SylvainSylvain
3092 silver badges5 bronze badges
Well, much as I like the list[s] version, here's another more verbose way I found [but it's cool so I thought I'd add it to the fray]:
>>> text = "My hovercraft is full of eels"
>>> [text[i] for i in range[len[text]]]
['M', 'y', ' ', 'h', 'o', 'v', 'e', 'r', 'c', 'r', 'a', 'f', 't', ' ', 'i', 's', ' ', 'f', 'u', 'l', 'l', ' ', 'o', 'f', ' ', 'e', 'e', 'l', 's']
answered Feb 9, 2015 at 4:07
2
from itertools import chain
string = 'your string'
chain[string]
similar to list[string]
but returns a generator that is lazily evaluated at point of use, so memory efficient.
answered Jul 16, 2018 at 10:19
minggliminggli
991 silver badge5 bronze badges
1
string="footbar"
print[[*string]]
It is easy way to split a string word by word.
answered Sep 5 at 16:45