Bytes Methods
bytes.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
| Name | Type | Description |
|---|---|---|
| prefix | bytes-like | tuple[bytes-like, ...] | Candidate binary sequence or tuple of alternatives. |
| start | int | None | Inclusive search start; negative values count from the end. |
| end | int | None | Exclusive 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(bytes(b'PK:DATA.bin').startswith(b'PK:'))print(bytes(b'PK:DATA.bin').startswith((b'XX', b'PK:')))print(bytes(b'PK:DATA.bin').startswith(b'DATA', 3, 7))