• Unicode in Python

Question related to mission Periodic Table


Since this tasks is specifically asking for Unicode characters, here is a bit of advise, in particular if you like to test your code on your local computer before submitting it to CheckIO.

Source encoding

If you want to write Unicode in your source code as-is, make sure that your source code file is saved in UTF-8 format and add the following to line 1 or line 2 of your source file:

  #!/usr/bin/env python
  # encoding: utf-8


  #!/usr/bin/env python3.3
  # -*- coding: utf-8 -*-

(See PEP 263 for details.)

Output encoding

If you want to print Unicode, I assume that you actually want to output UTF-8 to stdout. print() actually takes a Unicode string, and silently converts that to the encoding, which can be found by examining sys.stdout.encoding. If your run Python from the terminal, it usually does a pretty good job of guessing this encoding. If the guessing of the encoding fails, you will get the following error:

UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-9: ordinal not in range(128)

Unfortunately, the most obvious fix does NOT work:

sys.stdout.encoding = 'utf-8'   # Fails with AttributeError: readonly attribute

There are three ways to fix this, in order of preference:

1) In the terminal, set the environment variable:


2) Create a wrapper around stdout and stderr:

if sys.stdout.encoding != 'UTF-8':
    sys.stdout = codecs.getwriter('utf-8')(sys.stdout.buffer, 'strict')
if sys.stderr.encoding != 'UTF-8':
    sys.stderr = codecs.getwriter('utf-8')(sys.stderr.buffer, 'strict')

(for maximum compatibility, you can replace the static 'utf-8' with locale.getpreferredencoding())

3) Encode each call to print() and sys.stdout.write()