mayankkaizen
mayankkaizen

Reputation: 327

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?

Upvotes: 0

Views: 640

Answers (2)

Classfied3D
Classfied3D

Reputation: 21

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

Upvotes: 2

chepner
chepner

Reputation: 530843

\u only accepts 4 digits, so your string literal is being parsed as two separate characters, \u1f60 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')
😀

Upvotes: 3

Related Questions