0

Final result should look like this:

string = "this is a simple text"
result = ["this", " ", "is", " ", "a", " ", "simple", " ", "text"]

1 Answers1

2

One way to do this would be to split with a regex and capture the delimiter:

import re

string = "this is a simple text"

re.split(r'(\s+)', string)
# ['this', ' ', 'is', ' ', 'a', ' ', 'simple', ' ', 'text']

Note, this will act a little different than str.split() on an empty string.

Mark
  • 90,562
  • 7
  • 108
  • 148