How to print Unicode character in Python?

I want to make a dictionary where English words indicate Russian and French translations.

How to print Unicode characters in Python? Also, how do you store unicode characters in a variable?

+94
python python-unicode
May 13 '12 at 5:00
source share
8 answers

To include Unicode characters in the Python source code, you can use Unicode delete characters in the form \u0123 in your string and the string literal prefix with 'u'.

Here is an example of working in the Python interactive console:

 >>> print u'\u0420\u043e\u0441\u0441\u0438\u044f'  

Lines declared like this are Unicode type variables, as described in the Python Unicode documentation .

If running the above command does not display the text correctly for you, your terminal may not be able to display Unicode characters.

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

Reading characters from a file in Python

+86
May 13 '12 at 5:10 AM
source share

Printing a unicode character in Python:

Printing a unicode character directly from a python interpreter:

 el@apollo:~$ python Python 2.7.3 >>> print u'\u2713' 

The unicode character u'\u2713' is a check mark. The interpreter prints a check mark on the screen.

Print unicode character from python script:

Put this in test.py:

 #!/usr/bin/python print("here is your checkmark: " + u'\u2713'); 

Run it as follows:

 el@apollo:~$ python test.py here is your checkmark: 

If it does not show a checkmark for you, the problem may be in another place, for example, the terminal settings or something that you do with redirecting the stream.

Save Unicode characters in a file:

Save this to a file: foo.py:

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

Run it and output the file to a file:

 python foo.py > tmp.txt 

Open tmp.txt and look inside, you will see the following:

 el@apollo:~$ cat tmp.txt e with obfuscation: é 

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

+43
Dec 07 '13 at 23:20
source share

If you try to print() Unicode and get ascii codec errors , check this page , TL; DR of which does export PYTHONIOENCODING=UTF-8 before starting python (this variable controls the sequence of bytes that the console tries to encode your string data as). Inside Python3, by default it uses UTF-8 (see Unicode HOWTO ), so that there is no problem; you can just put Unicode in lines as seen from other answers and comments. This is when you try to get this data to the console that the problem occurs. Python thinks that your console can only handle ascii. Some of the other answers say, “Write it to a file first,” but note that they define the encoding (UTF-8) for this (so Python doesn't change anything in writing), and then use the method to read the file, which just spills out bytes without any regard to encoding, so this works.

+25
May 15, '17 at 21:36
source share

In Python 2, you declare unicode strings with u , as in u"猫" , and use decode() and encode() to translate to and from Unicode, respectively.

This is pretty easy in Python 3. Here's a very good overview. This presentation has clarified a lot for me.

+18
May 13 '12 at 5:07
source share

I use Portable winpython on Windows, it includes the IPython QT console, I could achieve the following.

 >>>print ("結婚")結婚>>>print ("おはよう")おはよう>>>str = "結婚" >>>print (str)結婚 

your console interpreter must support unicode in order to display Unicode characters.

+4
Nov 18 '14 at 23:01
source share

Another thing that has not yet been added.

In Python 2, if you want to print a variable that has Unicode and use .format() , then do this (make the base string that is formatted with a Unicode string using u'' :

 >>> text = "Université de Montréal" >>> print(u"This is unicode: {}".format(text)) >>> This is unicode: Université de Montréal 
+1
Aug 29 '18 at 1:38
source share

This fixes printing UTF-8 in python:

 UTF8Writer = codecs.getwriter('utf8') sys.stdout = UTF8Writer(sys.stdout) 
0
Oct 08 '18 at 10:58
source share

Given that this is the first result when Google searches for this topic, it's worth mentioning that adding the u prefix to Unicode strings is optional in Python 3. (Python 2 example was copied from the top answer)

Python 3 (both work):

 print('\u0420\u043e\u0441\u0441\u0438\u044f') print(u'\u0420\u043e\u0441\u0441\u0438\u044f') 

Python 2:

 print u'\u0420\u043e\u0441\u0441\u0438\u044f' 
0
May 11 '19 at 16:22
source share



All Articles