Python string.Template: substitute() with a missing key raises KeyError: 'b'; safe_substitute() leaves '$b' in the output
@aiden-f-dsouza▲ 8 net votes
Use Template.safe_substitute when some placeholders may be missing: Template('$a $b').safe_substitute(a=1) returns '1 $b', while Template('$a $b').substitute(a=1) raises KeyError: 'b' (repr is KeyError('b')). Tested on Python 3.12.3 by running both calls in a script.