461

Consider:

$ 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?

1

2 Answers 2

876

In Python 3, UTF-8 is the default source encoding (see PEP 3120), so Unicode characters can be used anywhere.

In Python 2, you can declare in the source code header:

# -*- coding: utf-8 -*-
....

This is described in PEP 0263.

Then you can use UTF-8 in strings:

# -*- coding: utf-8 -*-

u = 'idzie wąż wąską dróżką'
uu = u.decode('utf8')
s = uu.encode('cp1250')
print(s)
Sign up to request clarification or add additional context in comments.

17 Comments

now it gives """UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 1: ordinal not in range(128)"""
You need not use unicode(), simply write string in UTF-8 encoding.
In Python versions older than 3, you also need to prefix unicode string literals with "u": some_string = u'idzie wąż wąską dróżką'.
on a diffrent string I am getting """UnicodeEncodeError: 'charmap' codec can't encode characters in position 1845-1846: character maps to <undefined>"""... does that mean a different encoding is required?
or #!/usr/bin/env python # coding: utf-8
|
92

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.

6 Comments

Is this needed for python3? I know python3 assumes all literals within the code are unicode. But does it assume the source files are also written in utf8?
@RicardoCruz Yes I believe utf-8 is the default for Python 3. See python.org/dev/peps/pep-3120
@ricardo-cruz With Python 3, all strings will be Unicode strings, so the original encoding of the source will have no impact at run-time. 1. PEP 3120 -- Using UTF-8 as the default source encoding 2. PEP 263 -- Defining Python Source Code Encodings
@noobninja thanks for the links: PEP 3120 confirms that the source code itself is now assumed to be UTF-8, not just strings.
Use # coding: utf8 instead of # -*- coding: utf-8 -*-which is far easier to remember.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.