如何在Python中打印上标?

536 阅读1分钟

问题的提出

  • 给出两个字符串x和y。
  • 创建一个新的字符串xy并将其打印到shell中。

请考虑以下例子。

INPUT

Jupyter笔记本的解决方案

这里给出了一个简单的方法。

from IPython.display import display, Math
display(Math('hello^{finxter}'))

这里给出了一个一般的方法--只需用你自己的变量替换xy

from IPython.display import display, Math
x = 'hello'
y = 'finxter'
display(Math(x + '^{' + y + '}'))

你可以在这里自己尝试。

点击在Jupyter笔记本中自己尝试(Google Colab)。

对于Python,一般的方法是行不通的。原因是Unicode没有提供对一般代码进行上标的方法。一个进一步研究的想法是使用一个Latex库,允许你在Python中编写和显示Latex代码。

The postHow to Print Superscript in Python?first appeared onFinxter.