如何在 jupyter 中打印希腊字母

Iva*_*van 4 jupyter-notebook

如果我说

plt.title(r'$\tau$')
Run Code Online (Sandbox Code Playgroud)

或者

plt.xlabel(r'$\tau$')
Run Code Online (Sandbox Code Playgroud)

jupyter笔记本中,我得到希腊字母 tau。

我如何对文本执行同样的操作?

这不起作用:

print(r'$\tau$', tau)
Run Code Online (Sandbox Code Playgroud)

kHa*_*hit 5

它在第一个示例中起作用的原因是 matplotlib支持任何 matplotlib 文本字符串中的 TeX 标记,但 python 不支持。

\n\n

尽管如此,在 python 中还有许多其他方法可以做到这一点。例如,您可以使用转义序列\\N{name}来打印 unicode 字符。

\n\n
>>> print(\'\\N{greek small letter tau}\')\n\xcf\x84\n
Run Code Online (Sandbox Code Playgroud)\n\n

或者你可以使用unicodedata.lookup

\n\n
>>> import unicodedata\n>>> print(unicodedata.lookup(\'greek small letter tau\'))\n\xcf\x84\n
Run Code Online (Sandbox Code Playgroud)\n\n
\n\n

另请注意, python 源代码的默认编码是 utf-8,因此您可以简单地在字符串文字中包含 unicode 字符,例如

\n\n
>>> print(\'\xcf\x84\')\n\xcf\x84\n
Run Code Online (Sandbox Code Playgroud)\n

  • 如果您知道所需字符的 Unicode 值(例如 τ 是十进制 964 或十六进制 03c4),您还可以使用 Unicode 转义,例如“u\03c4”。当您只需要偶尔使用符号并且不想找到其键盘等效项时,这非常方便。 (2认同)