[Tutor] Tutor Digest, Vol 100, Issue 58

Steven D'Aprano steve at pearwood.info
Tue Jun 26 03:22:54 CEST 2012


Alan Gauld wrote:
> Hi Emile,
> 
> On 26/06/12 00:26, Emile van Sebille wrote:
>>> Try using the operator that checks for equality such as
>>> if w[0] == 'x':
>>
>> This does the same thing and works even when w is an empty string.
> 
> Umm, so does startswith()? Can you clarify what you mean by the last 
> sentence?


I think you've messed up your quoting. It was Mike Nickey, not Emile, who 
suggested using w[0] == 'x'.

As Emile said, startswith is better because it works when w is an empty 
string, while the w[0]=='x' test fails:

py> w = ''
py> w.startswith('x')
False
py> w[0] == 'x'
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
IndexError: string index out of range



-- 
Steven


More information about the Tutor mailing list