Printing greek letters using sympy in text

Let's 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'm having trouble putting it in the text. for example

sympy.pprint("I am"+pi)

      

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

I + pi

+3


source to share


3 answers


You want pretty()

which is the same as pprint

, but it returns a string instead of printing it. In the meantime, there is no need to know about it. ”

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

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

      



If all you want is to get a 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

      

+2


source


latex and str will return a string



>>> latex(pi)
'\\pi'
>>> str(pi)
'pi'

      

+1


source


You can use unicodedata.lookup

to get Unicode character. In your case, you would do the following:

import unicodedata
print("I am " + unicodedata.lookup("GREEK SMALL LETTER PI"))

      

This gives the following output:

I am π

      

If you want a capital letter, you must do unicode.lookup("GREEK CAPITAL LETTER PI"))

. You can replace PI

any Greek letter with the name.

0


source







All Articles