DEV Community


Posted on

Python non-English encoding

You probably learned how to use Python output "Hello, World! ". In English no problem. But if you output non-English characters, it is likely to encounter encoding problems.

If Python file if the encoding is not specified, an error occurs during the execution (only for older Python versions):

print("мир приветствий")

More execution output is:

File "", line 2
    SyntaxError: Non-ASCII character '\xe4' in file on line 2, but no encoding declared; see for details

To prevent the above error message, the solution is to add at the beginning of the file

#-*- coding: UTF-8 -*-

or the line:

# coding = UTF-8

Example (Python 2.0+)

# coding: UTF-8
print("мир приветствий")

Note: Python3.X source file using the UTF-8 encoding by default, so you can properly resolve non-English, without specifying the UTF-8 encoding.

Note: If you use an IDE, you also need to set the encoding of a good editor, such as Pycharm setup steps:

File > Settings > encoding in the search input box.
Find Editor> File encodings > a IDE Encoding and Project Encoding
Set utf-8.

Top comments (0)