Why don't python remove non breaking whitespace on .strip(' ')
but .split(' ')
splits on
the character?
Firstly these functions perform two different tasks:
' foo bar '.split() is ['foo','bar']
#a list of substrings without any white space (Note the default is the look for whitespace - see below)
' foo bar '.strip() is 'foo bar'
#the string without the beginning and end whitespace
.
When using strip(' ')
only spaces at the beginning and end are removed, although very similar to strip()
it's not quite the same, for example with tab \t
which is whitespace but isn't a space:
' \t foo bar '. strip() is 'foo bar'
' \t foo bar '. strip(' ') is '\t foo bar'
When using split(' ')
it "splits" the string into a list against each space, compared to split()
splitting the string into a list against each "whitespace". Consider 'foo bar'
(with two spaces between foo and bar).
'foo bar'.split() is ['foo', 'bar']
#Two spaces count as only one "whitespace"
'foo bar'.split(' ') is ['foo', '', 'bar']
#Two spaces split the list into THREE (seperating an empty string)
The subtlety being that two spaces, or several spaces and tabs, are counted as "one whitespace".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With