Slicing vs .startswith

David Eppstein eppstein at ics.uci.edu
Mon Sep 22 14:51:51 EDT 2003


In article <3F6F3E38.5221D7E7 at engcorp.com>,
 Peter Hansen <peter at engcorp.com> 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                      http://www.ics.uci.edu/~eppstein/
Univ. of California, Irvine, School of Information & Computer Science




More information about the Python-list mailing list