split() and string.whitespace

MRAB google at mrabarnett.plus.com
Fri Oct 31 21:13:22 CET 2008


On Oct 31, 6:57 pm, Marc 'BlackJack' Rintsch <bj_... at gmx.net> wrote:
> On Fri, 31 Oct 2008 11:53:30 -0700, Chaim Krause wrote:
> > I am unable to figure out why the first two statements work as I expect
> > them to and the next two do not. Namely, the first two spit the sentence
> > into its component words, while the latter two return the whole sentence
> > entact.
>
> > import string
> > from string import whitespace
> > mytext = "The quick brown fox jumped over the lazy dog.\n"
>
> > print mytext.split()
> > print mytext.split(' ')
>
> This splits at the string ' '.
>
> > print mytext.split(whitespace)
>
> This splits at the string '\t\n\x0b\x0c\r ' which doesn't occur in
> `mytext`.  The argument is a string not a set of characters.
>
> > print string.split(mytext, sep=whitespace)
>
> Same here.
>
<muse>
It's interesting, if you think about it, that here we have someone who
wants to split on a set of characters but 'split' splits on a string,
and others sometimes want to strip off a string but 'strip' strips on
a set of characters (passed as a string). You could imagine that if
Python had had (character) sets from the start then 'split' and
'strip' could have accepted a string or a set depending on whether you
wanted to split on or stripping off a string or a set.
</muse>



More information about the Python-list mailing list