'How to convert a polynomial user input into python readable form

I have the following input:

enter image description here

and I want to convert it into python readable form like this 'x**2 + 2*x + 2' so that I can use it as an input to other functions. I have:

def func_parse(func):
    return func.replace("^","**")

but this will only resolve the power part. I also want to add '*' in between '2x' to conform with the python syntax.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source