What is a “bytestring” (the `bytes` data type) in Python?

It is a common misconception that text is ASCII or UTF-8 or Windows-1252, and therefore bytes are text.

Text is only text, in the way that images are only images. The matter of storing text or images to disk is a matter of encoding that data into a sequence of bytes. There are many ways to encode images into bytes: JPEG, PNG, SVG, and likewise many ways to encode text, ASCII, UTF-8 or Windows-1252.

Once encoding has happened, bytes are just bytes. Bytes are not images anymore; they have forgotten the colors they mean; although an image format decoder can recover that information. Bytes have similarly forgotten the letters they used to be. In fact, bytes don’t remember whether they were images or text at all. Only out of band knowledge (filename, media headers, etcetera) can guess what those bytes should mean, and even that can be wrong (in case of data corruption).

so, in Python (Python 3), we have two types for things that might otherwise look similar; For text, we have str, which knows it’s text; it knows which letters it’s supposed to mean. It doesn’t know which bytes that might be, since letters are not bytes. We also have bytestring, which doesn’t know if it’s text or images or any other kind of data.

The two types are superficially similar, since they are both sequences of things, but the things that they are sequences of is quite different.

Implementationally, str is stored in memory as UCS-? where the ? is implementation defined, it may be UCS-4, UCS-2 or UCS-1, depending on compile time options and which code points are present in the represented string.


“But why”?

Some things that look like text are actually defined in other terms. A really good example of this are the many Internet protocols of the world. For instance, HTTP is a “text” protocol that is in fact defined using the ABNF syntax common in RFCs. These protocols are expressed in terms of octets, not characters, although an informal encoding may also be suggested:

2.3. Terminal Values

Rules resolve into a string of terminal values, sometimes called
characters. In ABNF, a character is merely a non-negative integer.
In certain contexts, a specific mapping (encoding) of values into a
character set (such as ASCII) will be specified.

This distinction is important, because it’s not possible to send text over the internet, the only thing you can do is send bytes. saying “text but in ‘foo’ encoding” makes the format that much more complex, since clients and servers need to now somehow figure out the encoding business on their own, hopefully in the same way, since they must ultimately pass data around as bytes anyway. This is doubly useless since these protocols are seldom about text handling anyway, and is only a convenience for implementers. Neither the server owners nor end users are ever interested in reading the words Transfer-Encoding: chunked, so long as both the server and the browser understand it correctly.

By comparison, when working with text, you don’t really care how it’s encoded. You can express the “Heävy Mëtal Ümlaüts” any way you like, except “Heδvy Mλtal άmlaόts”


The distinct types thus give you a way to say “this value ‘means’ text” or “bytes”.

Leave a Comment