'How to interpret a string as code in Python

How to make a function that can interpret a string as a code ?

var1 = 5
var2 = 10
var3 = 7

str = "(var1*var2)+var3"


result = calculate(str) # I need help to write this function

result should output 57



Sources

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

Source: Stack Overflow

Solution Source