Question

This code is taken from the Sympy tutorial:

init_printing(use_unicode=False, wrap_line=False, no_global=True)
x = Symbol('x')
r = integrate(x**2 + x + 1, x)

print(r)

The output is: x**3/3 + x**2/2 + x

It's correct, but in tutorial the output was:

 3    2
x    x
-- + -- + x
3    2 

How can I reach this form of output?

If necessary: IDE is pyCharm, python ver. is 3.3

Was it helpful?

Solution

You need a pretty printer pprint instead of normal print. Refer the Sympy Tutorial section Printing

>>> from sympy import *
>>> init_printing(use_unicode=False, wrap_line=False, no_global=True)
>>> x = Symbol('x')
>>> r = integrate(x**2 + x + 1, x)
>>> print(r)
x**3/3 + x**2/2 + x
>>> pprint(r)
 3    2    
x    x     
-- + -- + x
3    2

Note For non string objects, print statement (Python 2.X) or the print function (Python 3.X) converts the object to string using the rules for string conversion.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top