split(None)

Oliver Fromme olli at secnetix.de
Tue Nov 6 13:04:54 EST 2001


The library reference says the following about the "split"
string method:

 |  If sep is not specified or None, any whitespace string
 |  is a separator.

So I thought that foo.split() and foo.split(None) should
return the same result.  However:

   >>> "foo bar baz".split()
   ['foo', 'bar', 'baz']

   >>> "foo bar baz".split(None)
   ['foo bar baz']

The problem is, I have to specify None because I also need
to specify the optional second argument "maxsplit" (and in
my case, I cannot simply use a single space character,
because words can be separated by arbitrary whitespace).

So, next thing I tried was to specify maxsplit as a named
argument.  Surprise ...  didn't work as expected, either:

   >>> "foo bar baz".split(maxsplit = 1)
   ['foo', 'bar', 'baz']

Looks like I'll have to use re.split(), unless I'm missing
something (I guess I am).

Is there a bug in string.split(), or in the documentation,
or in my brain?

Regards
   Oliver (still python newbie and eager to learn)

PS:  This is Python 2.1.1, if it matters.

PPS:  string.split.__doc__ says:  "If maxsplit is given,
splits into at most maxsplit words".  This is clearly
wrong:  It splits into at most maxsplits + 1 words.

-- 
Oliver Fromme, secnetix GmbH & Co KG, Oettingenstr. 2, 80538 München
Any opinions expressed in this message may be personal to the author
and may not necessarily reflect the opinions of secnetix in any way.

"All that we see or seem is just a dream within a dream" (E. A. Poe)



More information about the Python-list mailing list