Language Syntax

Source encoding — Python syntax

Python decodes source as UTF-8 by default. A recognized encoding declaration can appear on the first line or on the second after a comment-only first line. This governs source decoding, not the default encoding for arbitrary file I/O.

Formal Syntax & Structure

python syntaxpython
# -*- coding: utf-8 -*-

Working Example

Example Snippetpython
source=b'# coding: latin-1\ntext="caf\xe9"'ns={}exec(compile(source,'<source>','exec'),ns)print(ns['text'])
Python Reference