Built-in Functions and Types
bytes()
Construct immutable binary data. An integer requests that many zero bytes; an iterable supplies integer byte values from 0 to 255. A buffer source is copied into bytes, and text requires an explicit encoding. Indexing yields an integer, slicing yields bytes. Negative lengths and out-of-range byte values raise ValueError; inappropriate sources raise TypeError. Custom objects can implement __bytes__().
Formal Syntax & Structure
python syntaxpython
bytes(source=b'')bytes(source, encoding, errors='strict')Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| source | int | iterable | buffer | str | Size, byte values, buffer or text; omit for empty bytes. |
| encoding | str | Required for text input; otherwise omit. |
| errors | str | Encoding error handler; default strict. |
Return Value
bytes
Immutable sequence of byte values.
Working Example
Example Snippetpython
payload = bytes([65, 66, 255])print(payload.hex())print(payload[0])print(bytes("سلام", "utf-8").decode("utf-8"))