[英]Python Unicode - Can't print some unicode characters
I was trying to print random Unicode characters in Jupyter Notebook.我试图在 Jupyter Notebook 中打印随机 Unicode 字符。 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.我记得至少在其他两种情况下,输出是某个字符后跟 0。
I am using Python 3.9 on Windows 8. What am I doing wrong?我在 Windows 8 上使用 Python 3.9。我做错了什么?
\\u\u003c/code> only accepts 4 digits, so your string literal is being parsed as two separate characters,
\ὠ
and 0
.
\\u\u003c/code>只接受 4 位数字,因此您的字符串文字被解析为两个单独的字符,
\ὠ
和0
。
>>> print('\u1f60')
ὠ
>>> print('0')
0
For larger code points, you need to use
\\U
(which requires 8 digits, including leading 0s).对于较大的代码点,您需要使用
\\U
(需要 8 位数字,包括前导 0)。
>>> 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您的代码的问题在于,当您使用 \\u 时,python 认为您使用的是 4 位数字,并认为末尾的 0 是字符串其余部分的一部分,因此请使用带有额外十六进制数字 (0) 的 \\U作为数字占位符
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.