[Tutor] avoid split function

David Rock david at graniteweb.com
Sat Aug 14 06:59:20 CEST 2004


* Kent Johnson <kent_johnson at skillsoft.com> [2004-08-13 10:54]:
> As long as I am being picky, I'll point out that there are quite a few 
> special cases to consider. I don't think either David or Chad's solution 
> duplicates the behavior of string.split() on all of them. (Hmm, sounds like 
> a good opportunity to learn about unittest :-)
> 
> >>> 'abc  de'.split()  # Multiple spaces between words
> ['abc', 'de']
> 
> >>> 'abc'.split()  # Just one word
> ['abc']
> 
> >>> ''.split()  # Empty string
> []
> 
> >>> 'abc  '.split()  # Trailing spaces
> ['abc']
> 
> >>> '  abc'.split()  # Leading spaces
> ['abc']

I had thought about most of these, but like I asked originally, there is
a reason that split() already exists. I suppose the most correct answer
would have been "read the split() source" :-)

-- 
David Rock
david at graniteweb.com
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
Url : http://mail.python.org/pipermail/tutor/attachments/20040813/434ac27e/attachment-0001.pgp


More information about the Tutor mailing list