Skip to Content
Introducing Python, 3rd Edition
book

Introducing Python, 3rd Edition

by Bill Lubanovic
September 2025
Beginner to intermediate
660 pages
12h 48m
English
O'Reilly Media, Inc.
Content preview from Introducing Python, 3rd Edition

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 is surrounded by quotes like a text string, but has a b right before the initial quote character. Each byte in it is specified either as an ASCII character or a two-character hex literal:

>>> b1 = b'ABC\x01\x02\x03\x41\x42\x43'
>>> b1
b'ABC\x01\x02\x03ABC'
>>> b2 = b'abc\x01\x02\x03\x61\x62\x63'
>>> b2
b'abc\x01\x02\x03abc'

This doesn’t mean that a bytes value can contain text characters. It means that you can specify a byte with its hex value or its ASCII equivalent, if it has one. Python allows this ASCII shortcut for convenience on input and displaying ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Introducing Python, 2nd Edition

Introducing Python, 2nd Edition

Bill Lubanovic

Publisher Resources

ISBN: 9781098174392Errata Page