Bytearray Methods

bytearray.find()

Locate the first matching byte offset. Searches accept a bytes-like object or a single integer byte (0–255). Bounds restrict complete matches; offsets are relative to the original sequence. Returns -1 when absent; use membership testing if no offset is needed. Empty patterns match boundaries, but a start greater than the original length does not match. Positions count bytes, not decoded characters. Integer search support was added in Python 3.3.

Formal Syntax & Structure

python syntaxpython
data.find(sub[, start[, end]])

Parameters & Attributes

NameTypeDescription
subbytes-like | intBinary subsequence or single byte value from 0 through 255.
startint | NoneInclusive search start; negative values count from the end.
endint | NoneExclusive search end; defaults to the end of the data.

Return Value

int

Absolute byte offset; -1 if absent.

Working Example

Example Snippetpython
print(bytearray(b'AB:AB:Z').find(b'AB'))print(bytearray(b'AB:AB:Z').find(58))print(bytearray(b'AB:AB:Z').find(memoryview(b'AB'), 1, 6))
Python Reference