Python Source Code Encoding

By Xah Lee. Date: . Last updated: .

Python 3 Source Code Encoding

Python 3's file source code encoding is UTF-8 . Make sure your editor save python files in that.

Python 2 Source Code Encoding

Python 2.x source code file encoding can be any, but is assumed to be ASCII by default.

If your source code contains non-ASCII characters, you must declare the file's encoding in the first line or second line, Like this:

-*- coding: utf-8 -*-

and you need to save the file in the encoding you declared. [see Set Text Editor File Encoding]

This convention came from emacs. [see Emacs: Declare File Encoding]

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

x = u"i ♥ cats"

print x

Python Unicode