O'Reilly logo

Python: Essential Reference, Third Edition by David Beazley

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Source Code Encoding

Python source programs are normally written in standard 7-bit ASCII. However, users working in Unicode environments may find this awkward—especially if they must write a lot of string literals.

It is possible to write Python source code in a different encoding by including a special comment in the first or second line of a Python program:

#!/usr/bin/env python
# -*- coding: UTF-8 -*-

name = u'M\303\274ller'  # String in quotes is directly encoded in UTF-8.

When the special coding: comment is supplied, Unicode string literals may be specified directly in the specified encoding (using a Unicode-aware editor program for instance). However, other elements of Python, including identifier names and reserved words, are still restricted ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required