Split and strip¶
From time to time you may want to split a string by a delimiting character like
,
, but the whitespace is all over the place.
>>> some_string = '0.1, 0.2,0.3, 0.4, 0.5'
>>> some_string.split(',')
['0.1', ' 0.2', '0.3', ' 0.4', ' 0.5']
>>> some_string.split(', ')
['0.1', ' 0.2,0.3', '0.4', ' 0.5']
So a pure splitting operation does not really do the trick. Write a function that improves this behavior for this case, so that you can use it as follows:
>>> strip_and_split(some_string, ',')
['0.1', '0.2', '0.3', '0.4', '0.5']
-
split_and_strip
(string, delimiter)¶ Return a list of stripped strings after splitting string by delimiter.
Parameters: Returns: The list of strings that are stripped after splitting by delimiter.
Return type:
Start by downloading the
exercise template
and editing
this file. You can run tests via
$ python split_and_strip.py test
to check whether you got a correct solution. You can also take a look at
one possible solution
.