all Python 3Byte sequencesfunction
bytes.startswith
bytes — search
Check whether the bytes start with a prefix.
Syntax
b.startswith(...)
The prefix must be a bytes-like object, or a tuple of them: calling b'GET /'.startswith('GET') with a text string raises TypeError, a classic slip when parsing network data. A tuple means 'any of these', and the optional start and end narrow the region without building a sliced copy, which beats b[2:5] == prefix on large buffers. It only tests; to strip the prefix use removeprefix (Python 3.9+).
Examples
print(b'hello'.startswith(b'he')) # → True
print(b'hello'.startswith((b'hi', b'he'))) # → True
print(b'hello'.startswith(b'llo', 2)) # → True
print(b'GET /index.html'.startswith(b'GET ')) # → True
print(b'hello'.startswith(b'')) # → True
print(b'hello'.startswith('he')) # → TypeError
See also
Common errors
Open in the full glossarydocs.python.orgReport an inaccuracy