Lets say I want to print something like
"I am 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)
which gives the greek letter pi, but I have problems putting this into a text. For example
sympy.pprint("I am"+pi)
obviously doesn't work. I can convert the text to a sympy symbol sympy.Symbol('I am'), but then I will get
I am+pi
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 π'
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
You can use unicodedata.lookup
to get the Unicode character. In your case you would do like this:
import unicodedata
print("I am " + unicodedata.lookup("GREEK SMALL LETTER PI"))
This gives the following result:
I am π
If you want the capital letter instead, you should do unicode.lookup("GREEK CAPITAL LETTER PI"))
. You can replace PI
with the name of any Greek letter.
latex and str will both return a string
>>> latex(pi)
'\\pi'
>>> str(pi)
'pi'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With