Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you print superscript in Python?

Tags:

I am aware of the \xb function in python, but it does not seem to work for me. I am aware that I may need to download a third party module to accomplish this, if so, which one would be best?

I am currently writing a binomial expansion solver, to try and use skills which I am teaching myself. The problem arises when I attempt to display the user input-ed expansion to the use for confirmation. Currently I am having to print the expression like so:

var1 = input("Enter a: ")
var2 = input("Enter b: ")
exponent = input("Enter n: ")

a = int(var1)
b = int(var2)
n = int(exponent)

expression = ('(%(1)dx%(2)d)^%(3)d') %\
{'1' : a, '2' : b, '3' : n}

print(expression)

confirmation = input(str("Is this correctt? Y/N "))

This prints (2x4)^5, whereas I'd prefer the index to be printed as superscript. How can this be done?

like image 470
Kage93 Avatar asked Dec 28 '11 02:12

Kage93


People also ask

How do you show a superscript in python?

If you use the _ symbol, the superscript will be under the character. If you use the ^ symbol, the superscript will be over the character.

How do you print squared in python?

You can also find the square of a given number using the exponent operator in python. It is represented by "**". While applying this method, the exponent operator returns the exponential power resulting in the square of the number.

How do you add a superscript to a string?

You need to use the ^ operator before the superscript. I presume you want to use this character string in a plot or Markdown so here's an example where the label of the X-axis contains a superscript.

How do you write a superscript in Jupyter notebook?

1 Answer. In Jupyter notebook, you can convert a cell into a code cell, markdown cell or a raw cell. You need a markdown cell for superscripts and subscripts. A markdown cell can be created by selecting a cell then pressing the Esc key followed by the M key.


1 Answers

You need to use a 'format' type thing. Use {}\u00b2".format(area))" and the{}becomes a²`. Here is an example:

print("The area of your rectangle is {}cm\u00b2".format(area))

The end of the code will print cm². You can change the large 2 at the end to other numbers for a different result. I do not know how to do a lower subscript though.

like image 172
James Avatar answered Dec 17 '22 08:12

James