This question already has an answer here:
$ cat bla.py
u = unicode('d…')
s = u.encode('utf-8')
print s
$ python bla.py
File "bla.py", line 1
SyntaxError: Non-ASCII character '\xe2' in file bla.py on line 1, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details
How can I declare utf-8 strings in source code?
In source header you can declare:
It is described in the PEP 0263:
Then you can use UTF-8 in strings:
This declaration is not needed in Python 3 as
UTF-8
is the default source encoding (see PEP 3120).In addition, it may be worth verifying that your text editor properly encodes your code in utf-8. Otherwise, you may have invisible characters that are not interpreted as utf-8.
Do not forget to verify if your text editor encodes properly your code in utf-8. Otherwise, you may have invisible characters that are not interpreted as utf-8.