我希望能够在python中获得str
转换函数,以充当sympy中的漂亮打印函数(或打印,就像使用任何参数调用init_printing()
一样)。现在,如果调用这样的函数,它只会更改打印到屏幕的print
函数。如何让str
表现为pprint
或其他选项?简而言之,能够以某种方式在字符串和python本身的变量中获取/截取print的输出是非常棒的。
例如,我希望能够做到:
from sympy import *
x,y=symbols('x y')
x_eq_y = Eq(x,2*y)
x_eq_y_str = str(x_eq_y) # holds 'Eq(x,2*y)' but I want it to hold 'x = 2y' or a latex formula etc
可以这样做吗?
答案 0 :(得分:1)
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner keyboard = new Scanner(System.in);
int operands, num;
int q = 1;
int a = 0;
String s = " ";
do {
System.out.println("Enter the number of operands (in range 2-10):");
operands = keyboard.nextInt();
} while ((operands < 2) || (operands > 10));
for (int number = 0; number <= operands - 1; number++) {
System.out.println("Enter number " + number + ":");
num = keyboard.nextInt();
s = s + num;
q = q * num;
}
System.out.print("Multiplication of" + s + " numbers is: " + q);
}
只是SymPy中提供的printing functions之一。如果您想使用另一个,请使用它:
str