0

Is there a way to print superscripts in python. Printing 0 or 2 as a superscript is not a problem but other than those yields normal digits.

For example, if I do:

>>> print("⁰¹²³⁴⁵⁶⁷⁸⁹")

I get

°1²345678?

Well, I seem to find no satisfying answer anywhere on the web. So, please help figure out a way around it...

By the way, I'm using python 3.5 on Windows 10 [32-bit], If that matters :-)

EDIT:

  • Could it be detected via Python if a shell supports UTF-8 encoding
  • Also, any way to ensure that we get the same output no matter what console we run in?
Melvin Abraham
  • 2,870
  • 5
  • 19
  • 33
  • 1
    this works as desired for me in py3 and py2 on linux 64-bit, btw – derricw Apr 27 '18 at 17:12
  • Related/possible dupe: [Convert numeric strings to superscript](https://stackoverflow.com/questions/13875507/convert-numeric-strings-to-superscript) – pault Apr 27 '18 at 17:15
  • Are you using Command Prompt? Seems to be a Command Prompt thing. Works fine in Idle. – palivek Apr 27 '18 at 17:16
  • I'm using PowerShell, btw – Melvin Abraham Apr 27 '18 at 17:17
  • Seems to be a limitation of PowerShell and CMD. Perhaps this [thread](https://stackoverflow.com/questions/388490/how-to-use-unicode-characters-in-windows-command-line/388500#388500) is of use. – palivek Apr 27 '18 at 17:26
  • @ palivek Thanks a lot, `chcp 65001` worked for me at once! Firstly typed `chcp 65001` in my shell right away and then ran my python script and **it worked** – Melvin Abraham Apr 27 '18 at 17:33
  • Do not use `chcp.com 65001`. You won't be able to input non-ASCII characters in the console. This is still broken in Windows 10. Upgrade to Python 3.6, which has proper support for wide-character console I/O. If you can't upgrade, then install and enable the win_unicode_console package. – Eryk Sun Apr 28 '18 at 09:48
  • Also, in Windows 7, setting the output codepage to 65001 causes a trail of garbage to be written after every write that includes non-ASCII characters. This is because WinAPI `WriteConsoleA` and `WriteFile` incorrectly return the number of UTF-16 elements written to the console instead of the number of UTF-8 bytes written. The two numbers are only the same for ASCII. This makes buffered writers retry writing the bytes that apparently weren't written successfully. This can cycle several times writing nonsense junk until the writer finally thinks all bytes were successfully written. – Eryk Sun Apr 28 '18 at 10:03

1 Answers1

0

I think it's a PowerShell problem.

If I try to print a 5 in superscript by using the corresponding code, it works only if I change the default font (Lucida Console) to another one like Consolas for instance.

>>> print(u'\u2075')
⁵
Romain
  • 19,910
  • 6
  • 56
  • 65