Distinguish Text from Bytes

Text represents characters. A bytes value is a sequence of integer elements from 0 through 255. Compare the two with one small UTF-8 example.

Line 4 of the shared measurement file is café,21.5. Its label "café" looks like four characters. Storage does not hold Python characters directly. Text mode reads stored bytes and decodes them into a Python string according to the stated encoding.

This lesson inspects that boundary once. The aim is not to design binary file formats. It is to keep two kinds of value distinct: text the program can read as characters, and bytes that can be stored or transmitted.

A String Contains Characters

A Python str value is text. Iterating over "café" produces four character strings:

<class 'str'>
4
['c', 'a', 'f', 'é']

The length is four because the program sees four characters. Indexing returns another string:

A bytes literal uses a leading b. Its elements are integers from 0 through 255, so indexing returns an integer:

Q1. Identify the value returned by byte indexing

Consider the ordinary ASCII bytes value data = b"caf". What does data[0] return?

Choose one

Select one choice, then check.

HintRetrieve the byte-element rule

The ASCII example immediately above shows the value and type returned at position zero.

SolutionByte indexing returns an integer

data[0] returns 99. Each element of a bytes value is an integer from 0 through 255.

Not attempted
Review

Not marked done.

UTF-8 Connects Text and Bytes

An encoding defines how text becomes bytes. The measurement file states UTF-8, so we can apply that same rule explicitly:

<class 'bytes'>
[99, 97, 102, 195, 169]
5

The four-character string becomes five bytes. The first three characters each use one byte in UTF-8. The character é uses two. Character count and byte count therefore answer different questions:

ValueKindElementsLength
"café"str"c", "a", "f", "é"4 characters
"café".encode("utf-8")bytes99, 97, 102, 195, 1695 bytes

This does not mean every non-ASCII character always uses two bytes. The result shown here belongs to this text and UTF-8. The important rule is to measure the representation the question asks about.

Q2. Count characters and UTF-8 bytes

For label = "café", enter the number returned by len(label.encode("utf-8")).

Compute it first, then check your number.

HintCount the encoded elements

The encoded list is [99, 97, 102, 195, 169].

SolutionThe encoded length is five

The bytes value has five integer elements, so len(label.encode("utf-8")) returns 5.

Not attempted
Review

Not marked done.

Decode with the Same Rule

Decoding performs the reverse operation:

The encoding name matters on both sides. In the ordinary strict path used here, Python raises an error rather than silently inventing text when the bytes do not follow the stated rule. Chapter 7's debugging method still applies: keep the failing bytes and operation visible instead of guessing at a replacement.

Text and binary file modes preserve this distinction:

ModeReading returnsWriting accepts
"r" or "w"strstr
"rb" or "wb"bytesbytes

The b means binary. In text mode, Python performs the encode or decode step using the selected encoding. In binary mode, the program works with the bytes directly.

Verify an Exact Binary Round Trip

For one known value, we can write the encoded bytes, read them back, compare them exactly, and then decode:

The first comparison checks the stored bytes. The second checks that strict UTF-8 decoding recovers the original text. These are two visible claims, not an assumption that a successful write must have preserved the value.

Q3. Complete a strict UTF-8 round trip

Replace the two question marks so the program encodes the label before writing and decodes the bytes after reading. Both checks must print True.

Editable Python

Command/Ctrl + Enter. Python runs in your browser.

Ready to run.

HintUse one encoding name in both directions

Call encode on the string and decode on the bytes. Pass "utf-8" to both calls.

SolutionEncode before writing and decode after reading

The bytes comparison verifies storage. The text comparison verifies decoding.

Not attempted
Review

Not marked done.

A string is a sequence of characters; a bytes value is a sequence of integer elements. UTF-8 encoding and decoding connect the two representations, and exact read-back can verify the stored bytes. The measurement file now has a clear boundary: its UTF-8 bytes become text before field parsing begins.

Pause and reflect

In your own words, note what you understood, what remains unclear, or what you want to revisit. The note stays with this lesson.

0 of 3 exercises marked done

Review

Not marked done.