liveBook · Manning

Unicode

As explained in Python Workout: 50 ten-minute exercises

A character-representation standard that assigns abstract code points to characters across writing systems and symbols.

A single document may need Russian, Chinese, and English together. Unicode makes this possible by giving characters from different languages and eras a shared representation. section 2.18

Unicode assigns each character a unique number called a code point. UTF-8 translates those numbers into bytes using variable-length encodings: ASCII characters use one byte, while other characters may use two, three, or more. section 2.18

Python exposes Unicode through ordinary string operations, but file handling depends on whether the data is text or binary:

  • Python 3 strings contain Unicode characters and behave as sequences: they can be iterated, indexed, searched, and sorted. section 2.0 section 2.18
  • Text files are read as UTF-8-formatted Unicode strings; nontext files such as PDFs and JPEGs should be opened in binary mode instead. section 5.2
  • Binary mode returns bytes rather than text lines, so fixed-size reads are more appropriate than line iteration. section 5.2

Sorting characters by Unicode order

Use sorted to order the characters in a string, then join the resulting list with an empty string to produce a new string. section 2.18

with open(filename, 'rb') as f:     #1
    while True:
        one_chunk = f.read(1000)    #2
        if not one_chunk:
            break
        print(f'This chunk contains {len(one_chunk)} bytes')

Inspect a chunked binary-reading pattern that opens a file with rb, reads fixed-size byte groups, and stops when no bytes remain. section 5.2

How Python Workout: 50 ten-minute exercises covers it

How deep it goes
Explains code points, variable-length UTF-8 encoding, Python strings, and binary-file consequences.
Examples
Uses multilingual documents, Unicode-order string sorting, and chunked reads from binary files.
What you can do after
Operate Python text and binary file handling by choosing Unicode text mode or byte-oriented binary mode.
How it connects
Connects Unicode with strings, UTF-8, ASCII, code points, and binary files.

Also explained in

Compare how 8 books explain Unicode →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Python Workout: 50 ten-minute exercises. The section links point to the parts of the book it draws on.