Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #9112

Re: String concatenation vs. string formatting

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'python,': 0.01; 'python.': 0.04; 'context': 0.04; 'deferred': 0.07; 'formatting': 0.07; 'python': 0.08; '%s"': 0.09; 'plus,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'subject:string': 0.09; 'output': 0.11; 'from:addr:vinay_sajip': 0.16; 'message-id:@post.gmane.org': 0.16; 'sajip': 0.16; 'somehow,': 0.16; 'subject:formatting': 0.16; 'subject:vs.': 0.16; 'vinay': 0.16; 'seems': 0.20; 'variable': 0.21; 'primarily': 0.22; "doesn't": 0.22; 'converts': 0.23; 'fine,': 0.23; 'least,': 0.23; "shouldn't": 0.23; 'versions': 0.23; "python's": 0.25; 'times,': 0.25; 'writes:': 0.25; 'string': 0.26; "i'm": 0.27; 'bit': 0.28; 'variables': 0.29; 'strings,': 0.30; "won't": 0.32; 'andrew': 0.32; 'rather': 0.33; 'actually': 0.33; 'to:addr:python-list': 0.34; 'header:X-Complaints-To:1': 0.34; 'instead': 0.34; 'header:User-Agent:1': 0.34; 'there': 0.34; 'done.': 0.34; 'things': 0.34; 'uses': 0.35; "isn't": 0.35; 'supposed': 0.35; 'charset:us-ascii': 0.36; 'some': 0.37; 'but': 0.37; 'using': 0.37; 'received:org': 0.38; 'subject:: ': 0.38; 'cases,': 0.38; 'from:addr:yahoo.co.uk': 0.38; 'case': 0.39; 'should': 0.39; 'header:Mime-Version:1': 0.39; "there's": 0.39; 'to:addr:python.org': 0.39; 'needed.': 0.40; 'where': 0.40; 'matter': 0.61; 'url:10': 0.65; 'subject:. ': 0.66; 'design.': 0.73; 'it"': 0.84; 'optimisation': 0.84; 'zen': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Vinay Sajip <vinay_sajip@yahoo.co.uk>
Subject Re: String concatenation vs. string formatting
Date Sat, 9 Jul 2011 11:06:37 +0000 (UTC)
References <4E17661D.6020307@gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset=us-ascii
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host sea.gmane.org
User-Agent Loom/3.14 (http://gmane.org/)
X-Loom-IP 82.152.15.113 (Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.2.18) Gecko/20110614 Firefox/3.6.18)
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.796.1310209616.1164.python-list@python.org> (permalink)
Lines 35
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1310209616 news.xs4all.nl 21802 [2001:888:2000:d::a6]:48397
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:9112

Show key headers only | View raw


Andrew Berg <bahamutzero8825 <at> gmail.com> 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

http://plumberjack.blogspot.com/2010/10/supporting-alternative-formatting.html

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).

Regards,

Vinay Sajip

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: String concatenation vs. string formatting Vinay Sajip <vinay_sajip@yahoo.co.uk> - 2011-07-09 11:06 +0000

csiph-web