The code works perfectly fine on Python 3.
However, in Python 2, if you do not add a u
before a string literal, you are constructing a string of bytes. When one wants to combine a string of bytes and a string of characters, one either has to decode the string of bytes, or encode the string of characters. Python 2.x opted for the former. In order to prevent accidents (for example, someone appending binary data to a user input and thus generating garbage), the Python developers chose ascii
as the encoding for that conversion.
You can add a line
from __future__ import unicode_literals
after the #coding
declaration so that literals without u
or b
prefixes are always character and not byte literals.