Newbie question regarding string.split()

Tommy Grav tgrav at mac.com
Fri Apr 20 15:55:45 EDT 2007


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
> split.

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?

Cheers
  Tommy



More information about the Python-list mailing list