Tom Tom - 1 year ago 73
Python Question

Python split string with some items of the string in order

Surpose the string:


split by :



'ab c d e a b cdea b cd e abcdeabcde'

This means split string


by indexes


How to do this by Python? This question is similar to Split a list into parts based on a set of indexes in Python
If follow the answers the out put is :

['ab', 'c', 'd', 'ea', 'bcde', 'abcd', 'eabcdeabcdeabcde']

isnt the expected output.

Answer Source

This quite matches your expected output, but I don't know what's the logic when 'cdebbe' reaches the end.

s = 'abcdeabcdeabcdeabcdeabcdeabcde'
split = 'cdebbe'

result = []
for c in split:
    left, center, s = s.partition(c)
    if left:

print(' '.join(result)) # ab c d e a b cdea b cd e
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download