# Python | sympy.xreplace() method

• Last Updated : 26 Jul, 2019

With the help of `sympy.xreplace()` method, we can replace the variable x with any other variable of the functions in the mathematical expression without editing the whole expression by using `sympy.xreplace()` method.

Syntax : `sympy.xreplace()`
Return : Return the replaced value of x in the mathematical expression.

Example #1 :
In this example we can see that by using `sympy.xreplace()` method, we are able to replace the x variable of mathematical functions in the expression and return the updated expression.

 `# import sympy``from` `sympy ``import` `*` ` ` `x, y ``=` `symbols(``'x y'``)`` ` `f ``=` `sin(x) ``+` `cos(``2` `*` `x)``# Use sympy.xreplace() method``gfg ``=` `f.xreplace({``2` `*` `x : y})``    ` `print``(gfg)`

Output :

sin(x) + cos(y)

Example #2 :

 `# import sympy``from` `sympy ``import` `*` ` ` `x, y ``=` `symbols(``'x y'``)`` ` `f ``=` `log(sin(x)) ``+` `tan(cos(``2` `*` `x))``# Use sympy.xreplace() method``gfg ``=` `f.xreplace({ x : ``2` `*` `y})``    ` `print``(gfg)`

Output :

log(sin(2*y)) + tan(cos(4*y))

My Personal Notes arrow_drop_up