Python: Split a string, including the whitespace character -
i'm trying split given string component characters, while keeping whitespace characters
in place. example of should is:
input: 'the string' output: ['t', 'h', 'e', ' ', 's', 't', 'r', 'i', 'n', 'g']
i relatively new python (using 2.7.6) , aware of .split()
method. know assumes splitting on whitespace character (' ')
if delimiter not set. however, when specify delimiter empty (ie split characters) string.split('')
, returns valueerror: empty separator.
does have suggestion how split string, while keeping whitespace characters? understand accomplished loop going on every character in string , appending list, i'm hoping find way without doing so.
this first post on stackoverflow, understandable , has not been answered. thanks.
you can use cast list()
string:
>>> list('the string') ['t', 'h', 'e', ' ', 's', 't', 'r', 'i', 'n', 'g']