On this page
bytearray
Prev: breakpoint | Up: __builtins__ | Next: bytes
Rel: bool, int, float, complex, str
Ref: docs.python.org — bytearray
Tags: #public #python3 #builtins
Import & Availability
# Built-in (available globally without import)
from builtins import bytearray
Overview & Signature
bytearray(iterable_of_ints) -> bytearray
bytearray(string, encoding[, errors]) -> bytearray bytearray(bytes_or_buffer) -> mutable copy of bytes_or_buffer bytearray(int) -> bytes array of size given by the parameter initialized with null bytes
Common Methods & Attributes
bytearray.append(self, item, /)— Append a single item to the end of the bytearray.bytearray.capitalize(self, /)— Return a copy of B with only its first character capitalized (ASCII) and the rest lower-cased.bytearray.center(self, width, fillchar=b' ', /)— Return a centered string of length width.bytearray.clear(self, /)— Remove all items from the bytearray.bytearray.copy(self, /)— Return a copy of B.bytearray.count(...)— Return the number of non-overlapping occurrences of subsection 'sub' in bytes B[start:end].bytearray.decode(self, /, encoding='utf-8', errors='strict')— Decode the bytearray using the codec registered for encoding.bytearray.endswith(...)— Return True if the bytearray ends with the specified suffix, False otherwise.bytearray.expandtabs(self, /, tabsize=8)— Return a copy where all tab characters are expanded using spaces.bytearray.extend(self, iterable_of_ints, /)— Append all the items from the iterator or sequence to the end of the bytearray.bytearray.find(...)— Return the lowest index in B where subsection 'sub' is found, such that 'sub' is contained within B[start:end].bytearray.fromhex(string, /)— Create a bytearray object from a string of hexadecimal numbers.bytearray.hex(...)— Create a string of hexadecimal numbers from a bytearray object.bytearray.index(...)— Return the lowest index in B where subsection 'sub' is found, such that 'sub' is contained within B[start:end].bytearray.insert(self, index, item, /)— Insert a single item into the bytearray before the given index.bytearray.isalnum(self, /)— Return True if all characters in B are alphanumeric and there is at least one character in B, False otherwise.bytearray.isalpha(self, /)— Return True if all characters in B are alphabetic and there is at least one character in B, False otherwise.bytearray.isascii(self, /)— Return True if B is empty or all characters in B are ASCII, False otherwise.bytearray.isdigit(self, /)— Return True if all characters in B are digits and there is at least one character in B, False otherwise.bytearray.islower(self, /)— Return True if all cased characters in B are lowercase and there is at least one cased character in B, False otherwise.bytearray.isspace(self, /)— Return True if all characters in B are whitespace and there is at least one character in B, False otherwise.bytearray.istitle(self, /)— Return True if B is a titlecased string and there is at least one character in B, i.e. uppercase characters may only follow uncased characters and lowercase characters only cased ones. Return Falsebytearray.isupper(self, /)— Return True if all cased characters in B are uppercase and there is at least one cased character in B, False otherwise.bytearray.join(self, iterable_of_bytes, /)— Concatenate any number of bytes/bytearray objects.bytearray.ljust(self, width, fillchar=b' ', /)— Return a left-justified string of length width.bytearray.lower(self, /)— Return a copy of B with all ASCII characters converted to lowercase.bytearray.lstrip(self, bytes=None, /)— Strip leading bytes contained in the argument.bytearray.maketrans(frm, to, /)— Return a translation table usable for the bytes or bytearray translate method.bytearray.partition(self, sep, /)— Partition the bytearray into three parts using the given separator.bytearray.pop(self, index=-1, /)— Remove and return a single item from B.bytearray.remove(self, value, /)— Remove the first occurrence of a value in the bytearray.bytearray.removeprefix(self, prefix, /)— Return a bytearray with the given prefix string removed if present.bytearray.removesuffix(self, suffix, /)— Return a bytearray with the given suffix string removed if present.bytearray.replace(self, old, new, count=-1, /)— Return a copy with all occurrences of substring old replaced by new.bytearray.resize(self, size, /)— Resize the internal buffer of bytearray to len.bytearray.reverse(self, /)— Reverse the order of the values in B in place.bytearray.rfind(...)— Return the highest index in B where subsection 'sub' is found, such that 'sub' is contained within B[start:end].bytearray.rindex(...)— Return the highest index in B where subsection 'sub' is found, such that 'sub' is contained within B[start:end].bytearray.rjust(self, width, fillchar=b' ', /)— Return a right-justified string of length width.bytearray.rpartition(self, sep, /)— Partition the bytearray into three parts using the given separator.bytearray.rsplit(self, /, sep=None, maxsplit=-1)— Return a list of the sections in the bytearray, using sep as the delimiter.bytearray.rstrip(self, bytes=None, /)— Strip trailing bytes contained in the argument.bytearray.split(self, /, sep=None, maxsplit=-1)— Return a list of the sections in the bytearray, using sep as the delimiter.bytearray.splitlines(self, /, keepends=False)— Return a list of the lines in the bytearray, breaking at line boundaries.bytearray.startswith(...)— Return True if the bytearray starts with the specified prefix, False otherwise.bytearray.strip(self, bytes=None, /)— Strip leading and trailing bytes contained in the argument.bytearray.swapcase(self, /)— Return a copy of B with uppercase ASCII characters converted to lowercase ASCII and vice versa.bytearray.title(self, /)— Return a titlecased version of B, i.e. ASCII words start with uppercase characters, all remaining cased characters have lowercase.bytearray.translate(self, table, /, delete=b'')— Return a copy with each character mapped by the given translation table.bytearray.upper(self, /)— Return a copy of B with all ASCII characters converted to uppercase.bytearray.zfill(self, width, /)— Pad a numeric string with zeros on the left, to fill a field of the given width.
Usage
buf = bytearray(b"hello")
buf[0] = ord("H")
buf.extend(b" world")
assert bytes(buf) == b"Hello world"
Gotchas
- Mutable
bytescounterpart: Unhashable (cannot be adictkey), and indexingbuf[0]returns anint(0..255), not a 1-bytebytes.
REPL Inspect
help(bytearray)
Linked from
Related notes
- tuts/python3/__builtins__/__builtins__ Linked here; links to this page; shared tags: #builtins
- tuts/python3/__builtins__/breakpoint Linked here; links to this page; shared tags: #builtins
- tuts/python3/__builtins__/bytes Linked here; links to this page; shared tags: #builtins
- tuts/python3/__builtins__ links to this page; shared tags: #builtins
- tuts/python3/__builtins__/bool Linked here; shared tags: #builtins
- tuts/python3/__builtins__/complex Linked here; shared tags: #builtins
- tuts/python3/__builtins__/float Linked here; shared tags: #builtins
- tuts/python3/__builtins__/int Linked here; shared tags: #builtins
- tuts/python3/__builtins__/str Linked here; shared tags: #builtins
- tuts/c/builtins shared tags: #builtins