import sympy

def rationalize_coeffs(expr):
    for i in expr.atoms(sympy.Float):
        r = sympy.Rational(str(i)).limit_denominator(1000)
expr = expr.subs(i, str(r.p) + '/' + str(r.q))
return expr
if __name__ == '__main__':
    x, y, z = sympy.symbols('x y z')
expr = 0.0350877192982456 * x + 0.75 * y + 0.75 * z
print(rationalize_coeffs(expr))
How do I replace floats with rationals in a sympy expression?