'result: {long_var_name}'.format(long_var_name=long_var_name)
A similar option is possible with '%'-style formatting: 'result: %(long_var_name)s'%{"long_var_name":long_var_name}
-or-
'result: %(long_var_name)s'%dict(long_var_name=long_var_name)
The first of these alternatives is indeed shorter, so long as there's only a single parameter. If there are multiple parameters then the .format() solution will be shorter.My experience with '%(name)s' % dict formatting is that I would often forget the terminal 's', and write it as '%(name)'. For template-like strings, where I want named substitutions, I am very much in favor of {}-style formatting.
So if your co-worker insists on named substitutions, then s.format() is less verbose than s % dict.
Regarding logging, there's a section of how to use format templates in the logging system, at https://docs.python.org/3/howto/logging-cookbook.html#format... . In addition to using a LogRecord factory, you could reduce the overhead in old-style logging to only an extra constructor calls:
log.info(M('result: {} {}', one, two))
where M.__str__ forwards to str.format.Personally, I would like a way to pre-compile the format string:
fmt = format_compile("result: {} {} {}")
for result in results:
print(fmt(*result))
because currently (in Python 3.3) "%d %d %d" is about 40% faster than "{} {} {}".EDIT: Never mind: It can be faster to interpolate the string each time than to call a constructor:
% python3.3 -mtimeit 's="%s %s %s"' 's % (1, 3, 9)'
1000000 loops, best of 3: 0.36 usec per loop
% python3.3 -mtimeit 'class Spam:pass' 'Spam()'
10000 loops, best of 3: 23.6 usec per loop
I did not expect a factor of 65!