简体   繁体   English

Python:以字符串形式显示的指数字符

[英]Python: Exponent-Characters shown in string

I'm looking for a way, to generate a string, that contains for example "x³".我正在寻找一种方法来生成一个字符串,例如包含“x³”。 If I don't know the exponent (3), and I want to add it to the string, like this: "x^"+"3" doesn't work (of course).如果我不知道指数 (3),并且我想将它添加到字符串中,就像这样: "x^"+"3" 不起作用(当然)。 Is there a way, to add numbers, or possibly even characters as exponent to a string with python?有没有办法用python将数字或什至字符作为指数添加到字符串中?

You might want to look at sympy :你可能想看看sympy

import sympy
from sympy import init_printing

init_printing()

sympy.pprint(sympy.exp("x^3"))
 ⎛ 3⎞
 ⎝x ⎠
 ℯ    

 sympy.pprint(sympy.exp("x3"))
  x₃
  ℯ  

I am a little bit confused about the question, but I think you are asking how to do superscript.我对这个问题有点困惑,但我认为你在问如何做上标。 You can use $$ wrapped around the ^ character in a string.您可以使用 $$ 包裹在字符串中的^字符。

plt.xlabel('Volumetric Flor Wate (m$^3$/s)')
plt.ylabel('Head (m)')
long_title = "Series: 73.77 (m$^3$/s), Head: 55.54 m \n Paraller: 67.065 ($m^3$/s), Head: 45.0132 m"
plt.title(long_title)

and it will show like this.它会像这样显示。 在此处输入图片说明

print( 'Exponent:\t ' , a , '² = ' , a ** b , sep = '' )

This is a line of Python code downloaded from the book "Python in Easy Steps" source code website.这是从“Python in Easy Steps”一书源代码网站下载的一行Python代码。 I had to Copy and Paste that line of code into the Python 3.5.1 IDLE for it to run because I can't figure out what combination of keystrokes to use to enter the exponent superscript 2 for the variable a.我必须将该行代码复制并粘贴到 Python 3.5.1 IDLE 中才能运行,因为我无法确定使用哪种击键组合来输入变量 a 的指数上标 2。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM