Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python中变量的上标_Python_Variables_Unicode_Superscript - Fatal编程技术网

python中变量的上标

python中变量的上标,python,variables,unicode,superscript,Python,Variables,Unicode,Superscript,我想打印一个变量作为上标。我做了很多研究,很多人使用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

我想打印一个变量作为上标。我做了很多研究,很多人使用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")
>>> print(eval(r'"\u00b' + str(2) + '"'))
²
以上这些都没有像我一直在做的那样奏效

(unicode error) 'unicodeescape' codec can't decode bytes in position 5-9: truncated \uXXXX escape

一些帮助将不胜感激。

转义码
\u00b2
是单个字符;你想要什么

variable = variable.decode(u\"u00b")
>>> print(eval(r'"\u00b' + str(2) + '"'))
²
不那么复杂

>>> print(chr(0x00b2))
²
不过,这只适用于上标2和上标3;这个

范围(10)内的i的
>:
...   如果i==1:
...     打印(chr(0x00b9))

... elif 2如果您希望将整个变量上标,则必须使用
%
格式。如果尝试
.format()
它,它将只在第一个字母或数字上加上标,因为它使用括号将
$$
中的数字分组

我这样写代码:

print(r'$y=%f*x^{%f}$' % (var_A, var_B)
这样您就不必调用Unicode。我发现这种方法适用于图形标题、轴标题和图形图例

我希望这有帮助