PEP 8 explicitly tells to use startswith
, because of readability:
- Use ''.startswith() and ''.endswith() instead of string
slicing to check
for prefixes or suffixes.
startswith() and endswith() are cleaner and less error prone. For
example:
Yes: if foo.startswith('bar'):
No: if foo[:3] == 'bar':