Comparison problem

Peter Hansen peter at engcorp.com
Sat Nov 26 19:10:47 EST 2005


Tom Anderson wrote:
> On Sat, 26 Nov 2005, Chris wrote:
> 
>>   if item[0:1]=="-":
> 
> item[0:1] seems a rather baroque way of writing item[0]! I'd actually 
> suggest writing this line like this:

Actually, it's not so much baroque as it is safe... item[0] will fail if 
the string is empty, while item[0:1] will return '' in that case.

Of course, as you point out, .startswith() is the better approach anyway.

-Peter




More information about the Python-list mailing list