'Output of LaTeX symbols using f-strings

Please bear with me, as I don't quite understand the possible and impossible uses of f-strings.

Take the code:

pi = 3.14159265
print(f'pi on 2 decimals is: {pi:.2f}')

Which obviously outputs: pi on 2 decimals is: 3.14

Would it be possible to have the following output: 𝜋 on 2 decimals is: 3.14, with the usual pi symbol (instead of the 2 letters "pi"), using both print and f-string instructions?

Otherwise, what's stuck? Is it the use of f-string, or the use of print, or the mix of both?



Solution 1:[1]

You need to pass unicode like:

pi_value = 3.14159265
pi_unicode = "\u03C0"
print(f'{pi_unicode} on 2 decimals is: {pi_value:.2f}')
? on 2 decimals is: 3.14

The list of greek unicodes can be found here

Note, unicode variable names are also possible in Python 3:

Python 3 allows many unicode symbols to be used in variable names. Unlike Julia or Swift, which allow any unicode symbol to represent a variable (including emoji) Python 3 restricts variable names to unicode characters that represent characters in written languages. In contrast, Python 2 could only use the basic ASCII character set for variable names.

? = 3.14

Solution 2:[2]

Python doesn't understand LaTeX markup. But you can just write a literal ? symbol in the source code:

pi = 3.14159265
print(f'? on 2 decimals is: {pi:.2f}')

outputs

? on 2 decimals is: 3.14

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 mkrieger1