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: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.010 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; '(especially': 0.07; 'formatting': 0.07; 'python': 0.08; 'allocates': 0.09; 'instance.': 0.09; 'subject:string': 0.09; 'syntax': 0.11; 'wrote:': 0.15; 'benjamin': 0.16; 'placeholder': 0.16; 'str()': 0.16; 'stringio': 0.16; 'subject:formatting': 0.16; 'subject:vs.': 0.16; 'argument': 0.16; 'object,': 0.16; 'pm,': 0.16; "doesn't": 0.22; 'header:In-Reply-To:1': 0.22; 'once.': 0.23; 'string': 0.26; 'fine': 0.26; '(the': 0.28; 'fri,': 0.28; 'message- id:@mail.gmail.com': 0.28; 'constant': 0.29; 'over.': 0.30; 'time:': 0.32; 'list': 0.32; 'typically': 0.33; 'rather': 0.33; 'usually': 0.33; 'to:addr:python-list': 0.34; 'pretty': 0.35; 'things,': 0.37; 'using': 0.37; 'received:google.com': 0.38; 'received:209.85.161': 0.38; 'received:209.85': 0.38; 'subject:: ': 0.38; '8bit%:8': 0.38; 'case': 0.39; 'either': 0.39; "there's": 0.39; 'to:addr:python.org': 0.39; 'received:209': 0.40; 'third': 0.40; 'plus': 0.65; 'subject:. ': 0.66; 'noise': 0.67; 'dealing': 0.69; 'alternative': 0.70; 'are)': 0.84; 'clearer': 0.84; 'everything.': 0.84; 'fly,': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type:content-transfer-encoding; bh=hG8u3Y9heCSYThKXCzRXu0R+st+E4eUiWRokykyNiT8=; b=ZflN80bvUrQrAWOrKz1MHTRKhzKTm40s0UkPyunDNhksUes3TkqIQG1/0/n5q0P4YT 2INf9hwQv8SHlKaQBV0CozKJZX/V9pmMviVCxnU452EyqBjqAbMHuYTiQnopIv30+y2C opaSsRtnxceAlRX1P2neS07fmhEUU+sggTLAM= MIME-Version: 1.0 In-Reply-To: References: <4E17661D.6020307@gmail.com> From: Ian Kelly Date: Fri, 8 Jul 2011 15:38:18 -0600 Subject: Re: String concatenation vs. string formatting To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 35 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1310161129 news.xs4all.nl 21758 [2001:888:2000:d::a6]:35783 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:9096 On Fri, Jul 8, 2011 at 3:23 PM, Benjamin Kaplan wrote: > String formatting is the One Right Way here.=A0It's fine to use string > concatenation for a few things, but=A0the operation is O(n^2) because eac= h > concat occurs=A0one at a time: Python allocates space for a string the si= ze of > the first 2 things, copies the contents over. Then=A0allocate a string th= e > size of that string plus the third string and copy the contents over. It = can > get pretty slow if you're building a really big string=A0With string > formatting, Python=A0creates a single string large enough to copy all=A0t= he > formatting arguements in and then copies=A0the contents over once. This argument doesn't really fly, because a string formatting operation is typically used as an alternative for a constant number of concatenations, not O(n) concatenations. As such, either approach would be O(n) for a single instance. In the case that you do need to do O(n) concatenations, it is usually best to use a StringIO object, or to build a list and then call str.join. > Also, string formatting (especially using the new syntax like you are) is > much clearer because there's less noise (the quotes all over the place an= d > the plusses) and=A0it's better for dealing with internationalization if y= ou > need to do that. This is the real reason to prefer string formatting over concatenation. It's also much less clutter to be able to use the %s placeholder rather than having to call str() on everything.