Q:

Recursive string substitution

x = "{app}_{filename}"

def fun():
    app = 'java'
    filename = 'gc.log'
    result = f'{x}'
    print(result) 
fun()

This will print "{app}_{filename}" but my goal is to substitute {app} and {filename} with local ones.  I have googled on how to do recursive variable substitution but didn't find anything.  

A:

You can use string format() to substitute the variables in your string template with something like:
x = "{app}_{filename}"

def fun(s):
    app = 'java'
    filename = 'gc.log'
    return s.format(app=app, filename=filename)

print(fun(x))
# prints: "java_gc.log"

