| 1 |
.. _bytes-ops: |
| 2 |
|
| 3 |
Native bytes operations |
| 4 |
======================== |
| 5 |
|
| 6 |
These ``bytes`` operations have fast, optimized implementations. Other |
| 7 |
bytes operations use generic implementations that are often slower. |
| 8 |
|
| 9 |
Construction |
| 10 |
------------ |
| 11 |
|
| 12 |
* Bytes literal |
| 13 |
* ``bytes(x: list)`` |
| 14 |
|
| 15 |
Operators |
| 16 |
--------- |
| 17 |
|
| 18 |
* Concatenation (``b1 + b2``) |
| 19 |
* Indexing (``b[n]``) |
| 20 |
* Slicing (``b[n:m]``, ``b[n:]``, ``b[:m]``) |
| 21 |
* Repetition (``b * n``, ``n * b``) |
| 22 |
* Comparisons (``==``, ``!=``) |
| 23 |
|
| 24 |
.. _bytes-methods: |
| 25 |
|
| 26 |
Methods |
| 27 |
------- |
| 28 |
|
| 29 |
* ``b.decode()`` |
| 30 |
* ``b.decode(encoding: str)`` |
| 31 |
* ``b.decode(encoding: str, errors: str)`` |
| 32 |
* ``b1.endswith(b2: bytes)`` |
| 33 |
* ``b.join(x: Iterable)`` |
| 34 |
* ``b1.startswith(b2: bytes)`` |
| 35 |
* ``b.translate(table: bytes)`` |
| 36 |
|
| 37 |
.. note:: |
| 38 |
|
| 39 |
:ref:`str.encode() <str-methods>` is also optimized. |
| 40 |
|
| 41 |
Formatting |
| 42 |
---------- |
| 43 |
|
| 44 |
A subset of % formatting operations are optimized (``b"..." % (...)``). |
| 45 |
|
| 46 |
Functions |
| 47 |
--------- |
| 48 |
|
| 49 |
* ``len(b: bytes)`` |
| 50 |
* ``ord(b: bytes)`` |