On Apr 20, 1:51 pm, kevinliu23 <[EMAIL PROTECTED]> wrote:
> Hey guys,
>
> So I have a question regarding the split() function in the string
> module. Let's say I have an string...
>
> input = "2b 3 4bx 5b 2c 4a 5a 6"
> projectOptions = (input.replace(" ", "")).split('2')
> print projectOptions
>
> ['', '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.

Also, you shouldn't use "input" as a variable name since it is a built-
in variable.

One hack to make it work is to add the following line right before you
print "projectOptions":

projectOptions.pop(0)  # pop the first element out of the list



Mike


-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to