Open In App

Python | sympy.sympify() method

Last Updated : 18 Jun, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

With the help of sympy.sympify() method, we are able to convert the expression of string type to general mathematical expression.

Syntax : sympy.sympify()
Return : Return the expression.

Example #1 :
In this example we can see that by using sympy.sympify(), we are converting the string of expression into real mathematical expression.




# Import sympy
from sympy import *
  
# Define symbols and trigo expression
x, y = symbols('x y')
expn = "x**2 + 3 * x - 1 / 2"
  
# Use sympy.sympify() method
gfg = sympify(expn)
  
print(gfg)


Output :

x**2 + 3*x – 1/2

Example #2 :




# Import sympy
from sympy import *
  
# Define symbols and trigo expression
x, y = symbols('x y')
expn = "x**2 + 3 * x - 1 / 2"
  
# Use sympy.sympify() method
gfg = sympify(expn)
gfg = gfg.subs(x, 3)
  
print(gfg)


Output :

35/2


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads