python split string on multiple delimeters without regex

python, split, string

Solution

If you need multiple delimiters, `re.split` is the way to go.

Without using a regex, it's not possible unless you write a custom function for it.

Here's such a function - it might or might not do what you want (consecutive delimiters cause empty elements):

>>> def multisplit(s, delims):
...     pos = 0
...     for i, c in enumerate(s):
...         if c in delims:
...             yield s[pos:i]
...             pos = i + 1
...     yield s[pos:]
...
>>> list(multisplit('hello there[my]friend', ' []'))
['hello', 'there', 'my', 'friend']

Problem

I have a string that I need to split on multiple characters without the use of regular expressions. for example, I would need something like the following: ``` >>>string="hello there[my]friend" >>>string.split(' []') ['hello','there','my','friend'] ``` is there anything in python like this?

Original source