Concepts / Network Communication and APIs

Network Communication and APIs

The bytes can then cross a storage or communication boundary.

  • Programming
Interactive lab

Try it: UTF-8: code points to bytes and back

How str.encode('utf-8') turns each character's Unicode code point into 1 to 4 bytes using fixed bit templates, and how bytes.decode('utf-8') reads them back — including the invalid bytes that raise UnicodeDecodeError.

How it works

  1. Every character is a Unicode code point, written U+XXXX (ord() gives its number).
  2. The code point's size picks a template: up to U+007F → 0xxxxxxx; up to U+07FF → 110xxxxx 10xxxxxx; up to U+FFFF → 3 bytes; above → 4 bytes.
  3. The code point's bits fill the x's from left to right, giving the bytes.
  4. Decoding reads a lead byte, which says how many 10xxxxxx continuation bytes follow, and joins their payload bits.
  5. A byte that cannot start a character, a wrong continuation byte, an overlong or surrogate form, or missing bytes at the end raise UnicodeDecodeError (errors='strict').

Default run (14 steps): 'Aé€😀'.encode("utf-8"): 4 characters, each becomes 1–4 bytes. … 4 characters → 10 bytes: b'A\xc3\xa9\xe2\x82\xac\xf0\x9f\x98\x80'. (bytes repr shows ASCII as text and every other byte as \xNN.)

Simplified: Up to 12 characters or 16 bytes; control characters and lone surrogates are removed from the text; only the strict error handler is shown.

Educational simulation

Loading the simulation…

Why English-Only Text Breaks Down

A program that handles only English text can work within ASCII, a limited character set covering letters, numbers, and basic punctuation. Real applications also encounter accented letters, Chinese characters, Arabic script, emoji, and text from many other languages. To handle that text correctly, a program needs Unicode and a reliable way to turn Unicode text into bytes for storage or transmission.

Key point

Unicode identifies characters. UTF-8 represents those characters as bytes when the text must be stored or transmitted.

Following a Character from Meaning to Bytes

Unicode stringcharacters and code pointsUTF-8 encodingcode points become bytesUTF-8 bytesdata for storage ortransmissionFile or networkoutside the programtext leaves programconvertwrite or send
How does text move from Unicode characters in a program to UTF-8 bytes sent across a network or written to a file?

Inside a Python program, Unicode text is handled as characters represented by Unicode code points. A code point is an abstract numerical identifier assigned to a character. That abstract identifier is not yet a sequence of bytes that a file or network connection can store or transmit. UTF-8 performs the conversion from code points to bytes. The bytes can then cross a storage or communication boundary.

Unicode and UTF-8 Compared

Unicodeabstract character setUTF-8concrete encoding methodCode pointunique numerical identifierByte sequencestorage or transmissionformassignsproducesencoded as
What is the difference between a character's Unicode code point and the UTF-8 byte sequence used to represent it?

Unicode is the abstract character set and standard. It assigns a unique code point to every character in every language and can represent over a million characters. UTF-8 is a variable-width encoding scheme that converts those code points into bytes. The two ideas work together, but they answer different questions: Unicode asks which character is meant, while UTF-8 asks how that character is represented as bytes.

QuestionUnicodeUTF-8
What does it define?Characters and their code pointsA byte representation for code points
What is its role?Abstract character set and standardConcrete encoding method
Where is it used?Inside a program to represent and manipulate textAt file, network, and other storage or transmission boundaries

Unicode and UTF-8 describe different stages of handling text.

Code Points and Variable-Width Bytes

AU+0041UTF-8 bytes1 byte for AéU+00E9UTF-8 bytes2 bytes for é😀U+1F600UTF-8 bytes3-4 bytes for Asian orother less commoncharactersencoded asencoded asencoded as
How does a character such as an accented letter or non-Latin symbol map from a code point to one or more UTF-8 bytes?

Unicode code points are commonly written with a U+ prefix followed by hexadecimal digits. The source examples include A at U+0041, é at U+00E9, and 😀 at U+1F600. UTF-8 uses a variable number of bytes: ASCII characters use one byte, European characters use two bytes, and Asian characters use three or four bytes. This lets common English characters remain compact while still supporting international text.

Worked example

Tracing the Character é

Explain the stages involved when a program handles the character é and later writes it to a file.

  1. 1
    Identify the character

    The program is working with the character é, not merely with an arbitrary byte.

  2. 2
    Use its Unicode identity

    Unicode assigns é the code point U+00E9. This code point identifies the character independently of the file or network representation.

  3. 3
    Encode at the boundary

    When the text is written to a file, UTF-8 converts the Unicode code point into bytes. The source describes European characters such as é as using two bytes in UTF-8.

  4. 4
    Store the bytes

    The resulting UTF-8 bytes are the concrete data written to storage. The same principle applies when text is sent over the internet.

Result

Unicode supplies the character identity; UTF-8 supplies the bytes needed by the file or communication boundary.

Unicode Strings in Python

python

In the source material, the u prefix creates a Unicode string. It tells Python to use the Unicode type rather than the regular string type. A Unicode string is handled internally using Unicode code points, allowing the program to represent and manipulate characters from many languages.

What do you think happens?

A program creates u"hello world". Has the program necessarily produced UTF-8 bytes for a file or network connection?

  • Yes, the u prefix performs the UTF-8 conversion

  • No, the u prefix creates Unicode text; UTF-8 is needed when the text crosses a storage or transmission boundary

Reveal answer
Answer:

No, the u prefix creates Unicode text; UTF-8 is needed when the text crosses a storage or transmission boundary

The source distinguishes Unicode strings used internally in Python from UTF-8 encoding used when reading from or writing to files and networks.

Encoding at Program Boundaries

A useful boundary rule is to keep text as Unicode while the program is working with it, then use UTF-8 when the text leaves the program for a file or network. When text comes back from a file or network, it must be converted back into Unicode so the program can work with the characters. The source describes this conversion as encoding when text leaves the program and decoding when text is read back into it.

File or networkinputUTF-8 bytesDecodebytes to UnicodeProgram textUnicode stringEncodeUnicode to UTF-8 bytesFile or networkoutputUTF-8 bytesreadbecomesprepare for outputwrite or send
Where in a practical program should text be encoded into UTF-8 and decoded back into Unicode?
python

When reading or writing files containing non-English text, specify encoding='utf-8' with the open function. This tells Python which conversion to use so Unicode strings are converted to UTF-8 bytes when written and converted back to Unicode when read. The exact syntax can depend on the Python version and file operation, so consult the current Python documentation for the operation you are using.

Mistakes with Text Representations

  • Common mistake

    Treating Unicode and UTF-8 as interchangeable names

    What it looks like

    Calling Unicode the byte format used to send text over a network

    Why it fails

    Unicode is the abstract character set and assigns code points. UTF-8 is the encoding method that turns those code points into bytes.

    Do this instead

    Describe the text internally as Unicode and identify UTF-8 as the representation used at storage and communication boundaries.

  • Common mistake

    Assuming the u prefix creates UTF-8 bytes

    What it looks like

    Assuming u"hello world" is already a network payload

    Why it fails

    The u prefix creates a Unicode string for internal handling. It does not by itself describe the byte representation needed outside the program.

    Do this instead

    Apply UTF-8 encoding when the text must be written to a file or sent over a network.

  • Common mistake

    Ignoring encoding for international files

    What it looks like

    Opening a file containing accented or non-Latin text without specifying an encoding

    Why it fails

    Python needs an encoding choice to convert between Unicode strings and file bytes correctly.

    Do this instead

    Specify encoding='utf-8' for the relevant file operation and check the current documentation for the exact syntax.

  • Common mistake

    Expecting every character to use one byte in UTF-8

    What it looks like

    Planning storage as if every international character had the same byte length as an ASCII character

    Why it fails

    UTF-8 is variable-width. ASCII characters use one byte, European characters use two bytes, and Asian characters use three or four bytes.

    Do this instead

    Remember that UTF-8 uses different numbers of bytes depending on the character.

Boundary Decisions

MEDIUM

For each situation, decide whether the program should primarily work with Unicode text or with UTF-8 bytes: manipulating a message inside Python, writing that message to a file, reading the file back, and sending the message over the internet. Explain what conversion occurs at each boundary.

Hints
  • Unicode is used internally for representing and manipulating text.
  • UTF-8 converts Unicode code points into bytes for storage and transmission.
  • Reading reverses the boundary conversion by turning bytes back into Unicode text.
Worked example

Choosing the Representation

A program receives international text, changes part of the message, and saves the result.

  1. 1
    Receive

    The incoming file or network data is represented as bytes, so it must be decoded into Unicode text for the program to handle the characters.

  2. 2
    Manipulate

    Keep the message as Unicode while changing or examining its characters inside the program.

  3. 3
    Save

    Encode the Unicode text as UTF-8 when writing it to the file or sending it onward.

Result

Use Unicode for internal text processing and UTF-8 at the input and output boundaries.

Key Takeaways

  1. 1
    Unicode is a universal character set and standard that assigns a code point to each character.
  2. 2
    UTF-8 is a variable-width encoding method that converts Unicode code points into bytes.
  3. 3
    The u prefix creates a Unicode string in the Python model described by the source material.
  4. 4
    Keep text as Unicode while processing it inside the program.
  5. 5
    Use UTF-8 when reading from or writing to files and when sending text across networks.

At a glance

  • Unicode identifies characters with code points; UTF-8 encodes those code points as bytes.
  • The Python u prefix creates a Unicode string for internal text handling.
  • UTF-8 is necessary when text crosses a file or network boundary.
  • UTF-8 is variable-width: ASCII characters use one byte, European characters use two bytes, and Asian characters use three or four bytes.
  • Specify UTF-8 for file operations involving international text and apply the same boundary principle to network communication.

Related concepts

More concepts starting with N