在文本中使用 sympy 打印希腊字母 [英] Printing greek letters using sympy in text

查看:128
本文介绍了在文本中使用 sympy 打印希腊字母的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我想打印类似的东西

Lets say I want to print something like

我是圆周率"

其中 pi 应该是希腊字母 pi.有了同情我可以做到

where pi should really be the greek letter pi. With sympy I can do

import sympy
from sympy.abc import pi
sympy.pprint(pi)

它给出了希腊字母 pi,但我在将其放入文本时遇到了问题.例如

which gives the greek letter pi, but I have problems putting this into a text. For example

sympy.pprint("I am"+pi)

显然不起作用.我可以将文本转换为同情符号 sympy.Symbol('I am'),但随后我会得到

obviously doesn't work. I can convert the text to a sympy symbol sympy.Symbol('I am'), but then I will get

我是+pi

推荐答案

你想要 pretty(),和 pprint 一样,但它返回一个字符串打印出来.

You want pretty(), which is the same as pprint, but it returns a string instead of printing it.

In [1]: pretty(pi)
Out[1]: 'π'

In [2]: "I am %s" % pretty(pi)
Out[2]: 'I am π'

如果您只关心获取 Unicode 字符,则可以使用 Python 标准库:

If all you care about is getting the Unicode character, you can use the Python standard library:

import unicodedata
unicodedata.lookup("GREEK SMALL LETTER %s" % letter.upper()) # for lowercase letters
unicodedata.lookup("GREEK CAPITAL LETTER %s" % letter.upper()) # for uppercase letters

这篇关于在文本中使用 sympy 打印希腊字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆