Code Reference

Bytes & Bytearray Methods

Python · Methods reference

Python · Methods reference


📋 Overview

bytes is immutable; bytearray is mutable with extra in-place methods.

🔧 Methods

Shared (bytes & bytearray)

MethodDescription
decode(encoding=…, errors=…)str from bytes
hex([sep])Hex string
fromhex(string)Class: bytes from hex (classmethod)
count(sub[, start[, end]])Count sub-sequence
find / rfind / index / rindexSub-sequence search
startswith / endswithPrefix/suffix on bytes
split(sep=None) / rsplit / partitionSplit bytes
strip / lstrip / rstripTrim bytes
replace(old, new[, count])Replace sub-sequence
join(iterable)Join byte sequences
maketrans / translateByte translation table

bytearray only

MethodDescription
append(i) / extend(iterable)Add bytes at end
insert(i, b) / remove(value) / pop([i])Mutate sequence
clear() / reverse()Clear or reverse in place

💡 Examples

See parent topic notes for runnable snippets; this page is the complete method index.

⚠️ Pitfalls

  • Mutating methods return None in Python — do not chain sort() / reverse() expecting a new list.
  • Default JS sort() compares strings — pass (a,b) => a-b for numbers.
  • SQL function names differ by dialect — verify Postgres vs MySQL docs.
  • Django QuerySet.update() skips save() signals and auto auto_now fields on models.

On this page