Slicing vs .startswith

David Eppstein eppstein at
Mon Sep 22 20:51:51 CEST 2003

In article <3F6F3E38.5221D7E7 at>,
 Peter Hansen <peter at> wrote:

> > For example, which one of the following would be more efficient, or ,
> > moreover, more pythonic?
> > 
> > if aa[:3] == 'abc':
> > 
> > vs
> > 
> > if aa.startswith('abc'):
> The latter is clearly more readable.

More Pythonic, too, I think.  "Readability counts," and "There should be 
one-- and preferably only one --obvious way to do it."  In this case, 
startswith must be the one obvious way, or else why would it exist in 
the standard library at all?

David Eppstein            
Univ. of California, Irvine, School of Information & Computer Science

More information about the Python-list mailing list