python 中变量的上标
Superscript for a variable in python
我想打印出一个变量作为上标。我做了很多研究,很多人都使用 Unicode 的方法。这类似于 print("word\u00b2")
,其中 2 将在上标中。但是,我想用一个变量替换 2 。这些是我试过的方法
print("word\u00b{variable}")
print("word\u00b{}".format(variable))
print("word\u00b%s" % variable)
当然还有反斜杠
print("word\u00b\{variable}")
print("word\u00b\{}".format(variable))
print("word\u00b\%s" % variable)
同样,我也试过
variable = variable.decode(u\"u00b")
None 上面的工作因为我不断得到
(unicode error) 'unicodeescape' codec can't decode bytes in position 5-9: truncated \uXXXX escape
我们将不胜感激。
转义码\u00b2
为单个字符;你想要像
这样的东西
>>> print(eval(r'"\u00b' + str(2) + '"'))
²
不那么复杂,
>>> print(chr(0x00b2))
²
不过,这仅适用于上标 2 和 3; other Unicode superscripts are in a different code block.
>>> for i in range(10):
... if i == 1:
... print(chr(0x00b9))
... elif 2 <= i <= 3:
... print(chr(0x00b0 + i))
... else:
... print(chr(0x2070 + i))
⁰
¹
²
³
⁴
⁵
⁶
⁷
⁸
⁹
如果您希望整个变量都带有上标,则必须使用 %
格式。如果您尝试 .format()
它,它只会在第一个字母或数字上标上标,因为它使用方括号对 $$
.
内的数字进行分组
我的代码是这样写的:
print(r'$y=%f*x^{%f}$' % (var_A, var_B)
这样就不用调用Unicode了。我发现此方法适用于图形标题、轴标题、图形图例。
希望对您有所帮助!
我想打印出一个变量作为上标。我做了很多研究,很多人都使用 Unicode 的方法。这类似于 print("word\u00b2")
,其中 2 将在上标中。但是,我想用一个变量替换 2 。这些是我试过的方法
print("word\u00b{variable}")
print("word\u00b{}".format(variable))
print("word\u00b%s" % variable)
当然还有反斜杠
print("word\u00b\{variable}")
print("word\u00b\{}".format(variable))
print("word\u00b\%s" % variable)
同样,我也试过
variable = variable.decode(u\"u00b")
None 上面的工作因为我不断得到
(unicode error) 'unicodeescape' codec can't decode bytes in position 5-9: truncated \uXXXX escape
我们将不胜感激。
转义码\u00b2
为单个字符;你想要像
>>> print(eval(r'"\u00b' + str(2) + '"'))
²
不那么复杂,
>>> print(chr(0x00b2))
²
不过,这仅适用于上标 2 和 3; other Unicode superscripts are in a different code block.
>>> for i in range(10):
... if i == 1:
... print(chr(0x00b9))
... elif 2 <= i <= 3:
... print(chr(0x00b0 + i))
... else:
... print(chr(0x2070 + i))
⁰
¹
²
³
⁴
⁵
⁶
⁷
⁸
⁹
如果您希望整个变量都带有上标,则必须使用 %
格式。如果您尝试 .format()
它,它只会在第一个字母或数字上标上标,因为它使用方括号对 $$
.
我的代码是这样写的:
print(r'$y=%f*x^{%f}$' % (var_A, var_B)
这样就不用调用Unicode了。我发现此方法适用于图形标题、轴标题、图形图例。
希望对您有所帮助!