How to split a string using an empty separator in Python

Question:

What is a good way to do some_string.split('') in python? This syntax gives an error:

a = '1111'
a.split('')

ValueError: empty separator

I would like to obtain:

['1', '1', '1', '1']

Answers:

Use list():

>>> list('1111')
['1', '1', '1', '1']

Alternatively, you can use map() (Python 2.7 only):

>>> map(None, '1111')
['1', '1', '1', '1']

Time differences:

$ python -m timeit "list('1111')"
1000000 loops, best of 3: 0.483 usec per loop
$ python -m timeit "map(None, '1111')"
1000000 loops, best of 3: 0.431 usec per loop
Answered By: TerryA

One can cast strings to list directly

>>> list('1111')
['1', '1', '1', '1']

or using list comprehensions

>>> [i for i in '1111']
['1', '1', '1', '1']

second way can be useful if one wants to split strings for substrings more than 1 symbol length

>>> some_string = '12345'
>>> [some_string[i:i+2] for i in range(0, len(some_string), 2)]
['12', '34', '5']
Answered By: oleg

Strings are iterables and can be indexed, hence you don’t really need to split it at all:

>>> for char in '11111':
...   print char
... 
1
1
1
1
1
>>> '11111'[4]
'1'

You can “split” it with a call to list, but it doesn’t make much difference:

>>> for char in list('11111'):
...   print char
... 
1
1
1
1
1
>>> list('11111')[4]
'1'

So you only need to do this if your code explicitly expects a list. For example:

>>> list('11111').append('2')
>>> l = list('11111')
>>> l.append(2)
>>> l
['1', '1', '1', '1', '1', 2]

This doesn’t work with a straight string:

>>> l.append('2')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'str' object has no attribute 'append'

In that case you would need:

>>> l += '2'
>>> l
'111112'
Answered By: Lennart Regebro

Method #1:

s="Amalraj"
l=[i for i in s]
print(l)

Output:

['A', 'm', 'a', 'l', 'r', 'a', 'j']

Method #2:

s="Amalraj"
l=list(s)
print(l)

Output:

['A', 'm', 'a', 'l', 'r', 'a', 'j']

Method #3:

import re; # importing regular expression module
s="Amalraj"
l=re.findall('.',s)
print(l)

Output:

['A', 'm', 'a', 'l', 'r', 'a', 'j']
Answered By: Amalraj Victory

If like me you arrived here to split in packets and not char by char, I made this :

>>> n=2
>>> str='423346396677674A446E6838436E304164334A30427874784467567863334942446E41424E413D3D'
>>> [ str[byte:byte+n] for byte in range(0,len(str)) if byte % n == 0 ]
['42', '33', '46', '39', '66', '77', '67', '4A', '44', '6E', '68', '38', '43', '6E', '30', '41', '64', '33', '4A', '30', '42', '78', '74', '78', '44', '67', '56', '78', '63', '33', '49', '42', '44', '6E', '41', '42', '4E', '41', '3D', '3D']
Answered By: Erwan Daniel

You can even omit the list() call altogether, with a combination of map + lambda fn and then use unpacking.

>>> s = "efgvdfbdb"
>>> [* map(lambda x: x, s) ]
['e', 'f', 'g', 'v', 'd', 'f', 'b', 'd', 'b']
Answered By: Kaustav Ganguly
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.