Python: Unicode Tutorial 🐍

By Xah Lee. Date: . Last updated: .

This page describe working with unicode in python 3.

[see Unicode: Character Set, Encoding, UTF-8, Codepoint]

Python 2 Unicode Tutorial

For Python 2, see Python 2: Unicode Tutorial

Python Source Code Encoding

Set Input/Output to UTF-8

Unicode in String

Python 3's string is a sequence of unicode characters. You do not need the u in u"", but you can add it for familiarity with python 2. The u has no meaning.

Unicode Characters in Variable and Function Names

Python 3 allows Unicode characters in variable and function names, but they must be letter characters. Non-letter characters are not allowed. [see What Characters Are Unicode Letter]

def φ(n):
    return n + 1

α = 4
# 5
♥ = 4


#     ♥ = 4
#     ^
# SyntaxError: invalid character in identifier
error: cannot format -: '♥'

Python Unicode