By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,908 Members | 1,860 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,908 IT Pros & Developers. It's quick & easy.

Inconsistent behavior of split on empty string

P: n/a
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
Share this Question
Share on Google+
1 Reply


P: n/a
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.