How to modify sympy's printing order?

Viewed 1785

I am using sympy to write algebraic expressions and perform basic calculations with them. Sympy does not keep track of the order of the variables which can be a problem when it comes to printing expressions (the issue has already been raised here and here, so this is not a duplicate). e.g.

>>> from sympy import *
>>> var("p,a")
>>> l=p-1-a;
>>> print(l);
-a+p-1

However, sympy seems to print the variables in the alphabetical order. Is there a way to change the alphebetical order Python refers to and thus trick sympy into printing the variables in the desired order? Any other solution is welcome!

2 Answers
Related