As of Python 3.5, % formatting will work for bytes, too!
This was part of PEP 461, authored by Ethan Furman:
PEP: 461
Title: Adding % formatting to bytes and bytearray
Version: $Revision$
Last-Modified: $Date$
Author: Ethan Furman <ethan at stoneleaf.us>
Status: Draft
Type: Standards Track
Content-Type: text/x-rst
Created: 2014-01-13
Python-Version: 3.5
Post-History: 2014-01-14, 2014-01-15, 2014-01-17, 2014-02-22, 2014-03-25,
2014-03-27
Resolution:
Abstract
========
This PEP proposes adding % formatting operations similar to Python 2's ``str``
type to ``bytes`` and ``bytearray`` [1]_ [2]_.
Rationale
=========
While interpolation is usually thought of as a string operation, there are
cases where interpolation on ``bytes`` or ``bytearrays`` make sense, and the
work needed to make up for this missing functionality detracts from the overall
readability of the code.
Motivation
==========
With Python 3 and the split between ``str`` and ``bytes``, one small but
important area of programming became slightly more difficult, and much more
painful -- wire format protocols [3]_.
This area of programming is characterized by a mixture of binary data and
ASCII compatible segments of text (aka ASCII-encoded text). Bringing back a
restricted %-interpolation for ``bytes`` and ``bytearray`` will aid both in
writing new wire format code, and in porting Python 2 wire format code.
Common use-cases include ``dbf`` and ``pdf`` file formats, ``email``
formats, and ``FTP`` and ``HTTP`` communications, among many others.
PEP 461 was accepted by Guido van Rossum on March 27, 2014:
Accepted. Congrats with marshalling yet another quite contentious discussion, and putting up with my last-minute block-headedness!
From this, we can obviously conclude that % is no longer scheduled for deprecation (as was announced with Python 3.1).
As of Python 3.5, % formatting will work for bytes, too!
This was part of PEP 461, authored by Ethan Furman:
PEP: 461
Title: Adding % formatting to bytes and bytearray
Version: $Revision$
Last-Modified: $Date$
Author: Ethan Furman <ethan at stoneleaf.us>
Status: Draft
Type: Standards Track
Content-Type: text/x-rst
Created: 2014-01-13
Python-Version: 3.5
Post-History: 2014-01-14, 2014-01-15, 2014-01-17, 2014-02-22, 2014-03-25,
2014-03-27
Resolution:
Abstract
========
This PEP proposes adding % formatting operations similar to Python 2's ``str``
type to ``bytes`` and ``bytearray`` [1]_ [2]_.
Rationale
=========
While interpolation is usually thought of as a string operation, there are
cases where interpolation on ``bytes`` or ``bytearrays`` make sense, and the
work needed to make up for this missing functionality detracts from the overall
readability of the code.
Motivation
==========
With Python 3 and the split between ``str`` and ``bytes``, one small but
important area of programming became slightly more difficult, and much more
painful -- wire format protocols [3]_.
This area of programming is characterized by a mixture of binary data and
ASCII compatible segments of text (aka ASCII-encoded text). Bringing back a
restricted %-interpolation for ``bytes`` and ``bytearray`` will aid both in
writing new wire format code, and in porting Python 2 wire format code.
Common use-cases include ``dbf`` and ``pdf`` file formats, ``email``
formats, and ``FTP`` and ``HTTP`` communications, among many others.
PEP 461 was accepted by Guido van Rossum on March 27, 2014:
Accepted. Congrats with marshalling yet another quite contentious discussion, and putting up with my last-minute block-headedness!
From this, we can obviously conclude that % is no longer scheduled for deprecation (as was announced with Python 3.1).
Another way would be:
"{0}, {1}, {2}".format(1, 2, 3).encode()
Tested on IPython 1.1.0 & Python 3.2.3
Videos
Decode the bytes object to produce a string:
>>> b"abcde".decode("utf-8")
'abcde'
The above example assumes that the bytes object is in UTF-8, because it is a common encoding. However, you should use the encoding your data is actually in!
Decode the byte string and turn it in to a character (Unicode) string.
Python 3:
encoding = 'utf-8'
b'hello'.decode(encoding)
or
str(b'hello', encoding)
Python 2:
encoding = 'utf-8'
'hello'.decode(encoding)
or
unicode('hello', encoding)
No. The idea is explicitly dismissed in the PEP:
For the same reason that we don't support
bytes.format(), you may not combine'f'with'b'string literals. The primary problem is that an object's__format__()method may return Unicode data that is not compatible with a bytes string.Binary f-strings would first require a solution for
bytes.format(). This idea has been proposed in the past, most recently in PEP 461. The discussions of such a feature usually suggest either
adding a method such as
__bformat__()so an object can control how it is converted to bytes, orhaving
bytes.format()not be as general purpose or extensible asstr.format().Both of these remain as options in the future, if such functionality is desired.
In 3.6+ you can do:
>>> a = 123
>>> f'{a}'.encode()
b'123'