Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Non breaking whitespace in python

Tags:

python

Why don't python remove non breaking whitespace on .strip(' ') but .split(' ') splits on the character?

like image 556
H.A Avatar asked Oct 06 '22 17:10

H.A


1 Answers

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".

like image 101
Andy Hayden Avatar answered Oct 10 '22 03:10

Andy Hayden