Split python string by predifned indices

Viewed 4728

I have a string that I'd like to split in specific places into a list of strings. The split points are stored in a separate split list. For example:

test_string = "thequickbrownfoxjumpsoverthelazydog"
split_points = [0, 3, 8, 13, 16, 21, 25, 28, 32]

...should return:

>>> ['the', 'quick', 'brown', 'fox', 'jumps', 'over', 'the', 'lazy', 'dog']

So far I have this as the solution, but it looks incredibly convoluted for how simple the task is:

split_points.append(len(test_string))
print [test_string[start_token:end_token] for start_token, end_token in [(split_points[i], split_points[i+1]) for i in xrange(len(split_points)-1)]]

Any good string functions that do the job, or is this the easiest way?

Thanks in advance!

3 Answers
Related