String concatenation vs. string formatting

Vinay Sajip vinay_sajip at
Sat Jul 9 13:06:37 CEST 2011

Andrew Berg <bahamutzero8825 <at>> writes:

> Other than the case where a variable isn't a string (format() converts
> variables to strings, automatically, right?) and when a variable is used
> a bunch of times, concatenation is fine, but somehow, it seems wrong.
> Sorry if this seems a bit silly, but I'm a novice when it comes to
> design. Plus, there's not really supposed to be "more than one way to do
> it" in Python.

In a logging context at least, using the form like

logger.debug("formatting message with %s", "arguments")

rather than

logger.debug("formatting message with %s" % "arguments")

means that the formatting is deferred by logging until it is actually needed. If
the message never gets output because of the logging configuration in use, then
the formatting is never done. This optimisation won't matter in most cases, but
it will in some scenarios.

By the way, logging primarily uses %-formatting instead of the newer
{}-formatting, because it pre-dates {}-formatting. In more recent versions of
Python, all of Python's three formatting styles are supported - see

Also by the way - Python doesn't say there shouldn't be more than one way to do
things - just that there should be one *obvious* way (from the Zen of Python).


Vinay Sajip

More information about the Python-list mailing list