all Python 3Byte sequencesfunction
bytearray.startswith
bytearray — search
Check whether the bytearray starts with the given prefix; a tuple of prefixes is accepted too.
Syntax
ba.startswith(...)
Pass a tuple of prefixes and one call replaces a chain of or-ed checks. Unlike find, this method rejects a bare int, and handing it a str raises TypeError — Python will not compare bytes against text. When you want to strip the prefix rather than test for it, removeprefix() (Python 3.9+) does that directly.
Examples
print(bytearray(b'hello').startswith(b'he')) # → True
print(bytearray(b'hello').startswith(b'lo')) # → False
print(bytearray(b'hello').startswith((b'ha', b'he'))) # → True
print(bytearray(b'hello').startswith(b'll', 2)) # → True
print(bytearray(b'hello').startswith(b'')) # → True
print(bytearray(b'hello').startswith('he')) # → TypeError
See also
Common errors
Open in the full glossarydocs.python.orgReport an inaccuracy