How to Replace variable value in string python?
Question: I working on python based project where formula's are stored in dB. My script
will read the formula's from a particular column and stored in list . For Eg

    
    
    formula[0] = "round(float(latitude[:2]) + (float(latitude[2:]) / 60)"
    formula[1] = "round(float(longitude[:3]) + float(longitude[3:]) / 60),6)"
    formula[3] = "int(float(speed)*1.852)"
    

Through TCP socket comma separated values will be coming like

    
    
    "imei:1234467454545,ac alarm,160302150105,,F,094605.000,A,1301.9905,N,08014.0746,E,0.19,298.01,,0,,,,;" 
    

by coding I have split the comma-separated values and stored in list. From the
stream `"1301.9905 "` is latitude, `"08014.0746"` is longitude and `"0.19"` is
speed value.

How can I apply the value in the formulas and store in some variable? I have
tried this method

    
    
    latitude = "1301.9905"
    latitude = round(float(latitude[:2]) + (float(latitude[2:]) / 60),6)
    print latitude
    


Answer: you can store the formula in the as a python template and then use `eval` to
execute it.

    
    
    from string import Template
    
    formula = "round((float($latitude) + float($latitude))/60)"
    template = Template(formula)
    out = template.substitute(latitude="1301.995")
    eval(out)
    

here i am using `eval` method to execute the function.

for more info on `templates` check
[here](https://docs.python.org/2/library/string.html#template-strings)

