Newbie question regarding string.split()
steve at holdenweb.com
Fri Apr 20 22:16:16 CEST 2007
Tommy Grav wrote:
> On Apr 20, 2007, at 3:15 PM, kyosohma at gmail.com wrote:
>> On Apr 20, 1:51 pm, kevinliu23 <kevinli... at gmail.com> wrote:
>>> ['', 'b34bx5b', 'c4a5a6']
>>> My question is, why is the first element of projectOptions an empty
>>> string? What can I do so that the first element is not an empty
>>> string? but the 'b34bx5b' string as I expected?
>>> Thanks so much guys. :)
>> The reason you have an empty string at the beginning is because you
>> are "splitting" on a character that happens to include the first
>> character in your string. So what you are telling Python to do is to
>> split the beginning from itself, or to insert a blank so that it is
> So why does this not happen when you use the empty split() function?
> [tgrav at Thrym] /Users/tgrav --> python
> Python 2.4.4 (#1, Oct 18 2006, 10:34:39)
> [GCC 4.0.1 (Apple Computer, Inc. build 5341)] on darwin
> Type "help", "copyright", "credits" or "license" for more information.
> >>> a = " 456 556 556"
> >>> a.split()
> ['456', '556', '556']
> >>> a.split(" ")
> ['', '456', '556', '556']
> What exactly does .split() use to do the splitting?
Any sequence of one or more whitespace characters. This is a rather
special case, quite different from .split(" ").
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
Recent Ramblings http://holdenweb.blogspot.com
More information about the Python-list