0

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?

2
  • 1
    Yes. You need to use the proper Unicode encoding which contains the exponent characters you want. Commented 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. Commented May 29, 2015 at 20:35

3 Answers 3

2

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₃
  ℯ  
2
  • @d0n.key, what do you mean by every string? Commented May 29, 2015 at 20:27
  • something like a^b to show mathematic formulas better.
    – d0n.key
    Commented May 30, 2015 at 10:14
2

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)

and it will show like this. enter image description here

1
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.