467,921 Members | 1,290 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,921 developers. It's quick & easy.

Inconsistent behavior of split on empty string

Hi,

I think the following behavior of the build in function 'split' is inconsistent.
What do you think?
"".split() [] "".split(";")

['']

I'm using python 2.3.3 on Windows 2000.
(Perl's split only returns all items up to the last non-empty item,
e.g. <perl>split /;/, "; ;;;"; gets you ['', ' ']. I find this confusing, too).
Jul 18 '05 #1
  • viewed: 1835
Share:
1 Reply
On 13 Feb 2004 00:46:00 -0800, bk***@gmx.de (bk***@gmx.de) wrote:
Hi,

I think the following behavior of the build in function 'split' is inconsistent.
What do you think?
"".split()[] "".split(";")['']

I'm using python 2.3.3 on Windows 2000.
(Perl's split only returns all items up to the last non-empty item,
e.g. <perl>split /;/, "; ;;;"; gets you ['', ' ']. I find this confusing, too).


..split() is not an information-preserving split. It is a convenient special case
whose function should not be confused with that of .split(something).

The latter would be buggy if you could not successfully do this:
anystring = ''
somestring = ';'
assert anystring == somestring.join(anystring.split(somestring))
anystring == somestring.join(anystring.split(somestring)) True

Unfortunately (other than making a likely bug announce itself),
somestring = ''
anystring == somestring.join(anystring.split(somestring))

Traceback (most recent call last):
File "<stdin>", line 1, in ?
ValueError: empty separator

but otherwise I think it works.

Regards,
Bengt Richter
Jul 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Robin Munn | last post: by
9 posts views Thread by Will McGugan | last post: by
1 post views Thread by jhcorey | last post: by
5 posts views Thread by kurt sune | last post: by
4 posts views Thread by Michele Petrazzo | last post: by
1 post views Thread by mad.scientist.jr | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.