How to print Unicode character in Python?

How to print Unicode character in Python?

To include Unicode characters in your Python source code, you can use Unicode escape characters in the form u0123 in your string. In Python 2.x, you also need to prefix the string literal with u.

Heres an example running in the Python 2.x interactive console:

>>> print uu0420u043eu0441u0441u0438u044f
Россия

In Python 2, prefixing a string with u declares them as Unicode-type variables, as described in the Python Unicode documentation.

In Python 3, the u prefix is now optional:

>>> print(u0420u043eu0441u0441u0438u044f)
Россия

If running the above commands doesnt display the text correctly for you, perhaps your terminal isnt capable of displaying Unicode characters.

These examples use Unicode escapes (u...), which allows you to print Unicode characters while keeping your source code as plain ASCII. This can help when working with the same source code on different systems. You can also use Unicode characters directly in your Python source code (e.g. print uРоссия in Python 2), if you are confident all your systems handle Unicode files properly.

For information about reading Unicode data from a file, see this answer:

Character reading from file in Python

Print a unicode character in Python:

Print a unicode character directly from python interpreter:

[email protected]:~$ python
Python 2.7.3
>>> print uu2713
✓

Unicode character uu2713 is a checkmark. The interpreter prints the checkmark on the screen.

Print a unicode character from a python script:

Put this in test.py:

#!/usr/bin/python
print(here is your checkmark:  + uu2713);

Run it like this:

[email protected]:~$ python test.py
here is your checkmark: ✓

If it doesnt show a checkmark for you, then the problem could be elsewhere, like the terminal settings or something you are doing with stream redirection.

Store unicode characters in a file:

Save this to file: foo.py:

#!/usr/bin/python -tt
# -*- coding: utf-8 -*-
import codecs
import sys 
UTF8Writer = codecs.getwriter(utf8)
sys.stdout = UTF8Writer(sys.stdout)
print(ue with obfuscation: é)

Run it and pipe output to file:

python foo.py > tmp.txt

Open tmp.txt and look inside, you see this:

[email protected]:~$ cat tmp.txt 
e with obfuscation: é

Thus you have saved unicode e with a obfuscation mark on it to a file.

How to print Unicode character in Python?

If youre trying to print() Unicode, and getting ascii codec errors, check out this page, the TLDR of which is do export PYTHONIOENCODING=UTF-8 before firing up python (this variable controls what sequence of bytes the console tries to encode your string data as). Internally, Python3 uses UTF-8 by default (see the Unicode HOWTO) so thats not the problem; you can just put Unicode in strings, as seen in the other answers and comments. Its when you try and get this data out to your console that the problem happens. Python thinks your console can only handle ascii. Some of the other answers say, Write it to a file, first but note they specify the encoding (UTF-8) for doing so (so, Python doesnt change anything in writing), and then use a method for reading the file that just spits out the bytes without any regard for encoding, which is why that works.

Leave a Reply

Your email address will not be published. Required fields are marked *