Sunday, December 4, 2016

Python: How to Split String with Empty Separator

First of all, this is on Python 2 point something something.


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 can't be done in Python (different algorithm). 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:
"...initiate a list object, get each string value using for, and copy it to the newly created object..."

No comments:

Post a Comment

Tell me what you think...