0

I've been trying to append a unit into a calculated value using the print statement in python 3, but I can't seem to find a way to get the unit to print as a superscript.

print ("I max = ", np.amax(intensity363), "J m$^-2$")

I've currently got tex, but I've tried html style with as I do it on a graph axes, and various other options listed in this post

How do you print superscript in Python?

but none work, given the age of this post I'm wondering if its a python 2/3 thing.

Thanks for the help

  • Why didn't this answer work for you? https://stackoverflow.com/a/8651412 – c2huc2hu Nov 29 '17 at 19:22
  • @user3080953. "assuming your console supports Unicode". – ekhumoro Nov 29 '17 at 19:27
  • I assume you are printing to a console? Then it will require your console to support that. Python (3) uses unicode natively. Your console may support unicode, but I doubt it will interpret tex or html, why would you think so? – juanpa.arrivillaga Nov 29 '17 at 19:34

2 Answers2

3

There's no such a built-in feature. You can use Unicode if you need some fancy symbols, e.g.

>>> print("%d⁻²" % 42)
42⁻²

You need to look up the different codes if you need something more dynamic than just the "-2".

Pavel
  • 7,436
  • 2
  • 29
  • 42
0

As I see that you're working with Jupyter you might be interested in this approach, if indeed you're not already using it.

embedded Latex

This is done in Py2 only because I haven't installed Py3 in Jupyter. I expect it works in essentially the same way.

Bill Bell
  • 21,021
  • 5
  • 43
  • 58