Skip to main content

Python: How to Split String with Empty Separator

First of all, this is on Python 2.7x.


For instance, we have this string:

If we use split with an empty string "" or '', it will return error.

ValueError


In JavaScript, we can directly use empty separator. Like so:

That cannot be done in Python. Below is some of the methods in Python.


Use list() Function

➡️ list() documentation


Use map() Function

➡️ map() documentation


Generic

String is an iterable object. So, we can directly use the index of each string and/or do iteration using for.

Let's initiate a list object, get each string value using for, and copy it to the newly created object:

That bit above means:

Initiate a list object, get each string value using for, and copy it to the newly created object.
PLATYC

Comments

Monkey Raptor uses cookies for analytics, advertisements, and functionality. More info on Privacy Policy