Slicing vs .startswith

Jeremy Fincher tweedgeezer at hotmail.com
Mon Sep 22 18:29:19 EDT 2003


Shu-Hsien Sheu <sheu at bu.edu> wrote in message news:<mailman.1064245938.12315.python-list at python.org>...
> For example, which one of the following would be more efficient, or , 
> moreover, more pythonic?
> 
> if aa[:3] == 'abc':
> 
> vs
> 
> if aa.startswith('abc'):

Python is about maintainability, and the latter is significantly more
maintainable than the former; if the string you're checking against
changes in size, using .startswith doesn't require you to change your
sourcecode anywhere else.

Jeremy




More information about the Python-list mailing list