On Apr 20, 11:51 am, 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']
>

The confusion, as you can see from other posts, is because the
behavior is different from default split().
Default split works on whitespace and  we don't get leading/trailing
empty list items.

So just add input = input.strip('2') after the input assignment (BTW
someone had
pointed input is a reserved identifier). Note this solution will work
for splitting on any sequence of chars..just strip them first. Note we
still get empty elements in the middle of the string -- this probably
we want to get in most cases.

Karthik

> 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. :)


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

Reply via email to