I'm looking for a way, to generate a string, that contains for example "x³". If I don't know the exponent (3), and I want to add it to the string, like this: "x^"+"3" doesn't work (of course). Is there a way, to add numbers, or possibly even characters as exponent to a string with python?
-
1Yes. You need to use the proper Unicode encoding which contains the exponent characters you want.– Dylan LawrenceCommented May 29, 2015 at 20:16
-
Where do you need it? If it's for example on a website, then there's html for that.– Stefan PochmannCommented May 29, 2015 at 20:35
Add a comment
|
3 Answers
You might want to look at sympy:
import sympy
from sympy import init_printing
init_printing()
sympy.pprint(sympy.exp("x^3"))
⎛ 3⎞
⎝x ⎠
ℯ
sympy.pprint(sympy.exp("x3"))
x₃
ℯ
I am a little bit confused about the question, but I think you are asking how to do superscript. You can use $$ wrapped around the ^
character in a string.
plt.xlabel('Volumetric Flor Wate (m$^3$/s)')
plt.ylabel('Head (m)')
long_title = "Series: 73.77 (m$^3$/s), Head: 55.54 m \n Paraller: 67.065 ($m^3$/s), Head: 45.0132 m"
plt.title(long_title)
print( 'Exponent:\t ' , a , '² = ' , a ** b , sep = '' )
This is a line of Python code downloaded from the book "Python in Easy Steps" source code website. I had to Copy and Paste that line of code into the Python 3.5.1 IDLE for it to run because I can't figure out what combination of keystrokes to use to enter the exponent superscript 2 for the variable a.