If you want to create dynamic SQL statements with Python variables, use Python string Template.
string Template supports $-based substitutions.
My previous tips on Python.