On this page
builtins.bytes
Prev: builtins.bytearray | Up: builtins | Next: builtins.BytesWarning
Rel: builtins.abs, builtins.aiter, builtins.all, builtins.anext
Ref: docs.python.org — builtins.bytes
Tags: #public #python3 #stdlib #builtins
Import
import builtins
from builtins import bytes
Signature & Overview
builtins.bytes(iterable_of_ints) -> bytes
bytes(string, encoding[, errors]) -> bytes bytes(bytes_or_buffer) -> immutable copy of bytes_or_buffer bytes(int) -> bytes object of size given by the parameter initialized with null bytes
Methods & Attributes
bytes.capitalize(self, /)— Return a copy of B with only its first character capitalized (ASCII) and the rest lower-cased.bytes.center(self, width, fillchar=b' ', /)— Return a centered string of length width.bytes.count(...)— Return the number of non-overlapping occurrences of subsection 'sub' in bytes B[start:end].bytes.decode(self, /, encoding='utf-8', errors='strict')— Decode the bytes using the codec registered for encoding.bytes.endswith(...)— Return True if the bytes ends with the specified suffix, False otherwise.bytes.expandtabs(self, /, tabsize=8)— Return a copy where all tab characters are expanded using spaces.bytes.find(...)— Return the lowest index in B where subsection 'sub' is found, such that 'sub' is contained within B[start,end].bytes.fromhex(string, /)— Create a bytes object from a string of hexadecimal numbers.bytes.hex(...)— Create a string of hexadecimal numbers from a bytes object.bytes.index(...)— Return the lowest index in B where subsection 'sub' is found, such that 'sub' is contained within B[start,end].bytes.isalnum(self, /)— Return True if all characters in B are alphanumeric and there is at least one character in B, False otherwise.bytes.isalpha(self, /)— Return True if all characters in B are alphabetic and there is at least one character in B, False otherwise.bytes.isascii(self, /)— Return True if B is empty or all characters in B are ASCII, False otherwise.bytes.isdigit(self, /)— Return True if all characters in B are digits and there is at least one character in B, False otherwise.bytes.islower(self, /)— Return True if all cased characters in B are lowercase and there is at least one cased character in B, False otherwise.bytes.isspace(self, /)— Return True if all characters in B are whitespace and there is at least one character in B, False otherwise.bytes.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 Falsebytes.isupper(self, /)— Return True if all cased characters in B are uppercase and there is at least one cased character in B, False otherwise.bytes.join(self, iterable_of_bytes, /)— Concatenate any number of bytes objects.bytes.ljust(self, width, fillchar=b' ', /)— Return a left-justified string of length width.bytes.lower(self, /)— Return a copy of B with all ASCII characters converted to lowercase.bytes.lstrip(self, bytes=None, /)— Strip leading bytes contained in the argument.bytes.maketrans(frm, to, /)— Return a translation table usable for the bytes or bytearray translate method.bytes.partition(self, sep, /)— Partition the bytes into three parts using the given separator.bytes.removeprefix(self, prefix, /)— Return a bytes object with the given prefix string removed if present.bytes.removesuffix(self, suffix, /)— Return a bytes object with the given suffix string removed if present.bytes.replace(self, old, new, count=-1, /)— Return a copy with all occurrences of substring old replaced by new.bytes.rfind(...)— Return the highest index in B where subsection 'sub' is found, such that 'sub' is contained within B[start,end].bytes.rindex(...)— Return the highest index in B where subsection 'sub' is found, such that 'sub' is contained within B[start,end].bytes.rjust(self, width, fillchar=b' ', /)— Return a right-justified string of length width.bytes.rpartition(self, sep, /)— Partition the bytes into three parts using the given separator.bytes.rsplit(self, /, sep=None, maxsplit=-1)— Return a list of the sections in the bytes, using sep as the delimiter.bytes.rstrip(self, bytes=None, /)— Strip trailing bytes contained in the argument.bytes.split(self, /, sep=None, maxsplit=-1)— Return a list of the sections in the bytes, using sep as the delimiter.bytes.splitlines(self, /, keepends=False)— Return a list of the lines in the bytes, breaking at line boundaries.bytes.startswith(...)— Return True if the bytes starts with the specified prefix, False otherwise.bytes.strip(self, bytes=None, /)— Strip leading and trailing bytes contained in the argument.bytes.swapcase(self, /)— Return a copy of B with uppercase ASCII characters converted to lowercase ASCII and vice versa.bytes.title(self, /)— Return a titlecased version of B, i.e. ASCII words start with uppercase characters, all remaining cased characters have lowercase.bytes.translate(self, table, /, delete=b'')— Return a copy with each character mapped by the given translation table.
Usage
b = bytes([65, 66, 67])
assert b == b"ABC" and b[0] == 65 and b.hex() == "414243"
Gotchas
- Indexing returns an
int, slicing returnsbytes:b"ABC"[0]is65, whileb"ABC"[0:1]isb"A".
REPL Inspect
from builtins import bytes
help(bytes)
Linked from
Related notes
- tuts/python3/stdlib_module/builtins/bytearray Linked here; links to this page; shared tags: #builtins, #stdlib
- tuts/python3/stdlib_module/builtins/BytesWarning Linked here; links to this page; shared tags: #builtins, #stdlib
- tuts/python3/stdlib_module/builtins Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/builtins/abs Linked here; shared tags: #builtins, #stdlib
- tuts/python3/stdlib_module/builtins/aiter Linked here; shared tags: #builtins, #stdlib
- tuts/python3/stdlib_module/builtins/all Linked here; shared tags: #builtins, #stdlib
- tuts/python3/stdlib_module/builtins/anext Linked here; shared tags: #builtins, #stdlib
- tuts/python3/stdlib_module/builtins/any shared tags: #builtins, #stdlib
- tuts/python3/stdlib_module/builtins/ArithmeticError shared tags: #builtins, #stdlib
- tuts/python3/stdlib_module/builtins/ascii shared tags: #builtins, #stdlib