简体   繁体   中英

Python Unicode - Can't print some unicode characters

I was trying to print random Unicode characters in Jupyter Notebook. In general, I am able to print characters but there are many which I can't print. Although I couldn't keep track of other examples, the following one is the I was able to reproduce -

str('\u1F600') #o/p is 'ὠ0' but actually it is a Grinning Face

I remember that in at least 2 other cases, the output was some character followed by 0.

I am using Python 3.9 on Windows 8. What am I doing wrong?

\\u\u003c/code> only accepts 4 digits, so your string literal is being parsed as two separate characters, \ὠ and 0 .

>>> print('\u1f60')
ὠ
>>> print('0')
0

For larger code points, you need to use \\U (which requires 8 digits, including leading 0s).

>>> print('\U0001f600')
😀

Try this:

print("\U0001f600")

Output:

😀

The problem with your code is that when you use \\u, python thinks you are using 4 digits, and thinks the the 0 at the end is part of the rest of the string, so use the \\U with extra hexadecimal digits (0) as digits a placeholder

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM