Is there any reason to prefer startswith over string slicing in Python?

Viewed 352

If foo="apple", then the following statements are equivalent:

foo.startswith("app")
foo[0:3] == "app"

In the sense that both will return "True".

Is there any reason to prefer one method over another? And by reason, I mean:

  1. Is one method significantly faster than the other?
  2. Is one method considered more or less 'Pythonic' than the other?
2 Answers

Slicing is a special case for a general purpose feature. If you'd have to check any other slice, other then from the start or from the end of string, then slices would do. But for these two cases startswith and endswith provide better readability.

As Zen Of Python states "Simple is better than complex" and "Readability counts". So foo.startswith("app") is a better choice, except for cases when efficiency really matters.

UPD: i have conducted a quick research. It appears, there is almost no time difference between these two approaches. Computation time differs in range of 5% and is basically neglectable.

Here is the research itself:

import random
import string

def get_random_string(length):
    letters = string.ascii_lowercase
    result_str = ''.join(random.choice(letters) for i in range(length))
    return result_str
    
txt = [get_random_string(random.randint(5, 20)) for i in range(1000)]

def time_slice():
    s = datetime.now()
    for i in txt:
        check = i[0:5]
        if i[0:5] == check:
            pass
    return (datetime.now() - s).microseconds

def time_func():
    s = datetime.now()
    for i in txt:
        check = i[0:5]
        if i.startswith(check):
            pass
    return (datetime.now() - s).microseconds

>>> sum(time_slice() for i in range(100)) / 100
... 316.15

>>> sum(time_func() for i in range(100)) / 100
... 303.08

Yeah... There is. One shows the intention more clearly. The first is more dynamic. all you have to do is pass the argument string. while the second not only needs you to change the string but also the range 0:3 to compensate for the length of the new string. the first is just cleaner.

Related