Bytearray Methods

bytearray.startswith()

Test whether the selected byte range begins with a literal binary sequence. A tuple allows alternative candidates; they are tried in order until one matches. A single integer is not accepted, unlike find(). Empty candidates match valid boundaries, but not a start beyond the data length. No text decoding or case conversion occurs.

Formal Syntax & Structure

python syntaxpython
data.startswith(prefix[, start[, end]])

Parameters & Attributes

NameTypeDescription
prefixbytes-like | tuple[bytes-like, ...]Candidate binary sequence or tuple of alternatives.
startint | NoneInclusive search start; negative values count from the end.
endint | NoneExclusive search end; defaults to the end of the data.

Return Value

bool

True if a candidate matches at the specified edge; False otherwise.

Working Example

Example Snippetpython
print(bytearray(b'PK:DATA.bin').startswith(b'PK:'))print(bytearray(b'PK:DATA.bin').startswith((b'XX', b'PK:')))print(bytearray(b'PK:DATA.bin').startswith(b'DATA', 3, 7))
Python Reference