Chapter 5. Bytes and Bytearray
I’m feeling a bit off.
Byte calling in sick
Python 3 introduced the following sequences of eight-bit integers, with possible values from 0 to 255, in two types:
-
An immutable sequence of 8-bit values:
bytes
-
A mutable sequence of 8-bit values:
bytearray
This chapter covers the basics of both types. Chapter 20 goes into more detail on their real-life uses, including binary file formats. You’ll probably deal with binary data much less often than text strings.
The official Python docs have all the details on these data types. I’m including just the most common and useful in this chapter.
Here’s one way to think of bytes versus strings:
-
Bytes are like equally-sized beads on a wire.
-
Strings are like a charm bracelet.
Bytes
Bytes, like strings, are immutable.
You can’t append, insert,
or change the contents of a bytes
value
after you’ve created it.
Create with Quotes
A literal bytes
object ...
Get Introducing Python, 3rd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.