Inconsistent behavior of split on empty string

Discussion in 'Python' started by bk_kl@gmx.de, Feb 13, 2004.

  1. Guest

    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).
    , Feb 13, 2004
    #1
    1. Advertising

  2. On 13 Feb 2004 00:46:00 -0800, () 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
    Bengt Richter, Feb 17, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tzury Bar Yochay
    Replies:
    1
    Views:
    400
    Gabriel Genellina
    Mar 24, 2008
  2. John Nagle
    Replies:
    10
    Views:
    413
    jhermann
    Aug 5, 2010
  3. Daniel Harple
    Replies:
    5
    Views:
    123
    Daniel Harple
    Feb 27, 2006
  4. Sam Kong
    Replies:
    5
    Views:
    236
    Rick DeNatale
    Aug 12, 2006
  5. Stanley Xu
    Replies:
    2
    Views:
    601
    Stanley Xu
    Mar 23, 2011
Loading...

Share This Page