Behaviour of str.split

Greg Ewing greg at cosc.canterbury.ac.nz
Wed Apr 20 01:33:15 EDT 2005


Will McGugan wrote:
> Hi,
> 
> I'm curious about the behaviour of the str.split() when applied to empty 
> strings.
> 
> "".split() returns an empty list, however..
> 
> "".split("*") returns a list containing one empty string.

Both of these make sense as limiting cases.

Consider

 >>> "a b c".split()
['a', 'b', 'c']
 >>> "a b".split()
['a', 'b']
 >>> "a".split()
['a']
 >>> "".split()
[]

and

 >>> "**".split("*")
['', '', '']
 >>> "*".split("*")
['', '']
 >>> "".split("*")
['']

The split() method is really doing two somewhat different things
depending on whether it is given an argument, and the end-cases
come out differently.

-- 
Greg Ewing, Computer Science Dept,
University of Canterbury,	
Christchurch, New Zealand
http://www.cosc.canterbury.ac.nz/~greg



More information about the Python-list mailing list