How do you print a superscript in Python?

I am aware of the \ xb function in python, but it doesn't seem to work for me. I know that I may need to download a third-party module for this, if so, which one is better? I am a noob with Python, and with StackOverflow, therefore, my main question. Now a little about the context ...

I am currently writing a binomial solver to try and use the skills that I teach myself. The problem occurs when I try to display the user I / O extension to use for confirmation. Currently, I have to print the expression as follows:

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, while I prefer the index to print as superscript. How can this be done? If necessary, I will provide any (missing) information.

+6
source share
6 answers

You can use the sympy module, which does the necessary formatting for you. It supports many formats, such as ascii, unicode, latex, mathml, etc.:

 from sympy import pretty_print as pp, latex from sympy.abc import a, b, n expr = (a*b)**n pp(expr) # default pp(expr, use_unicode=True) print(latex(expr)) print(expr.evalf(subs=dict(a=2,b=4,n=5))) 

Exit

  n (a*b) n (a⋅b) $\left(ab\right)^{n}$ 32768.0000000000 
+8
source

You are using input() , so I assume this is the console. You have two options for this, as discussed earlier here . One of them is to use a bit of formatting stroke to display the exponents on the line above the actual extension. Another is to use these great characters if your console supports Unicode:

 ⁰¹²³⁴⁵⁶⁷⁸⁹ 

You may have to increase the font size a bit to be legible, but this is certainly a viable option, with proper support. In addition, you mentioned that this is a personal learning experience; why not combine it with another and explore the simpler aspects of Pygame? It is very simple, text manipulation and keyboard events cannot be simpler, and it will never be the wrong step for branching.

+4
source

You need to use a type of type "format". 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 big 2s at the end to other numbers for a different result. However, I do not know how to make a subscript.

+3
source

In Python 3.6+ (mentioned only because the example uses f-lines that are not available in previous versions), called Unicode characters, provide ease of writing, an easy to read way to do this. Here is a list.

Example:

 f'\N{GREEK SMALL LETTER GAMMA}={density:.2f} t/m\N{SUPERSCRIPT THREE}' 

gives something like

 γ=1.20 t/m³ 
+1
source

Your Python program probably works as a console application that can only print characters without formatting. A simple answer to your question: "you cannot do this."

Of course, you can write a GUI application, our way out to a document format that supports formatting (RTF, HTML, TeX, etc.), but this, most likely, goes beyond the scope of the homework problem you are working on.

0
source

As already mentioned in MK, you cannot format the output on the command line (besides some colors and bold / blinking depending on the terminal). However, you can write it in a way that your users are likely to understand. If they are from the academic sector, you can use the latex style to express add-ins, for example. x^2

0
source

Source: https://habr.com/ru/post/904575/


All Articles