Il modo comune è la format()funzione:
>>> s = "This is an {example} with {vars}".format(vars="variables", example="example")
>>> s
'This is an example with variables'
Funziona bene con una stringa di formato multilinea:
>>> s = '''\
... This is a {length} example.
... Here is a {ordinal} line.\
... '''.format(length='multi-line', ordinal='second')
>>> print(s)
This is a multi-line example.
Here is a second line.
Puoi anche passare un dizionario con variabili:
>>> d = { 'vars': "variables", 'example': "example" }
>>> s = "This is an {example} with {vars}"
>>> s.format(**d)
'This is an example with variables'
La cosa più vicina a ciò che hai chiesto (in termini di sintassi) sono le stringhe del modello . Per esempio:
>>> from string import Template
>>> t = Template("This is an $example with $vars")
>>> t.substitute({ 'example': "example", 'vars': "variables"})
'This is an example with variables'
Dovrei aggiungere però che la format()funzione è più comune perché è prontamente disponibile e non richiede una riga di importazione.
vars()olocals()come dizionario in questione