class Template(string.Formatter):

    def format_field(self, value, spec):
        if spec.endswith('h'):
            value = cgi.escape(value)
spec = spec[:-1] + 's'
return super(Template, self).format_field(value, spec)
print(Template().format('{0:h} {1:d}', '<hello>', 123))
Add custom conversion types for string formatting