Python Source Code Encoding

By Xah Lee. Date: . Last updated: .

Python 3's file source code encoding is UTF-8.

Make sure your editor save python files in UTF-8. (usually there's a preference setting.)

If you don't know what is Unicode, you should read Unicode Basics: Character Set, Encoding, UTF-8, Codepoint

Python 2 Unicode Characters in Source Code, UTF8 Declaration

For Python 2.x, the source code file encoding 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 UTF-8 encoding.

This declaration is a convention adopted from the text editor Emacs . It tells any program reading the file that the file is encoded using a particular encoding.

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

x = u"i ♥ cats"

print x

Python Unicode

Python

Overview

Help

Comment

String

Arithmetic

Boolean

Conditional

Loop, Iteration

Data Structure

Function and Class

Module

Misc

Regex

Text Processing

Web

Misc