Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!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.024 X-Spam-Evidence: '*H*': 0.95; '*S*': 0.00; 'subject:: [': 0.03; 'formatting': 0.07; 'parsing': 0.09; 'pieces': 0.09; 'runtime': 0.09; 'subject:Tutor': 0.09; 'wrote:': 0.14; 'end"': 0.16; 'more:': 0.16; 'subject:] ': 0.16; 'stuff': 0.18; 'repeated': 0.19; 'tue,': 0.20; 'header:In-Reply-To:1': 0.22; '(and': 0.22; "what's": 0.24; 'thanks': 0.29; 'string': 0.29; 'probably': 0.30; 'equally': 0.31; 'strings.': 0.31; 'subject:working': 0.31; 'url:articles': 0.31; "skip:' 10": 0.32; 'to:addr:python-list': 0.32; 'usual': 0.32; 'idea': 0.32; 'from:charset:iso-8859-1': 0.33; 'operations': 0.33; 'received:192': 0.34; 'received:192.168.0': 0.35; 'normally': 0.35; 'like:': 0.35; 'optimization': 0.36; 'list,': 0.36; 'considered': 0.36; 'received:192.168': 0.37; 'two': 0.37; 'received:209.85': 0.37; 'subject:with': 0.37; 'apr': 0.38; 'steven': 0.38; 'received:google.com': 0.38; 'less': 0.38; 'but': 0.38; 'software': 0.38; 'to:addr:python.org': 0.39; 'received:209': 0.39; 'header:Mime-Version:1': 0.39; "it's": 0.40; 'header:Received:5': 0.40; '2011': 0.62; 'message- id:@localhost.localdomain': 0.74; 'concatenate': 0.84; 'overheard': 0.84; 'received:209.85.210.174': 0.84; 'received :mail-iy0-f174.google.com': 0.84; 'joel': 0.93 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:subject:from:to:in-reply-to:references :content-type:date:message-id:mime-version:x-mailer :content-transfer-encoding; bh=HnUvba+LqfVwVw3RAx25N1NJGud9iPDcY51CnJa4k7I=; b=T+p40JDShNJisBy5XFyV5jvh7t/aWN+R2KiUltMvv+qPzW3uAFZaSxeL3oND8atnua JY5QomtzIKeCSxPV15C00Vnccul4Fe9bJ7wPSPqBZDipODqvIOTf7oUINv59bmMd7gps WYnSwcdoTVD2PuZaV+v74oJW2Z4p7mWCODYBg= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=subject:from:to:in-reply-to:references:content-type:date:message-id :mime-version:x-mailer:content-transfer-encoding; b=sXkXXMKyO1YYYfQC68cX3nKizedAg3WYEvWGX0H5fyBViw4jI/Icmud7mUU8F8vGdE KTl3RU7u4bePrKt81zVnzAuZX/CbAU9T3culWi3XnbslHukhRa38g63ebSc9vq0RgO02 TjGxNFsIHt06NxXwkau32jDJWNjSPY/+82+c0= Subject: Re: [Tutor] working with strings in python3 From: Westley =?ISO-8859-1?Q?Mart=EDnez?= To: python-list@python.org In-Reply-To: <4dacf094$0$29984$c3e8da3$5496439d@news.astraweb.com> References: <4dacf094$0$29984$c3e8da3$5496439d@news.astraweb.com> Content-Type: text/plain; charset="UTF-8" Date: Mon, 18 Apr 2011 20:26:23 -0700 Mime-Version: 1.0 X-Mailer: Evolution 2.32.2 Content-Transfer-Encoding: 7bit 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: 49 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1303183591 news.xs4all.nl 41117 [::ffff:82.94.164.166]:43137 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:3525 On Tue, 2011-04-19 at 02:16 +0000, Steven D'Aprano wrote: > On Tue, 19 Apr 2011 10:34:27 +1000, James Mills wrote: > > > Normally it's considered bad practise to concatenate strings. > > *Repeatedly*. > > There's nothing wrong with concatenating (say) two or three strings. > What's a bad idea is something like: > > > s = '' > while condition: > s += "append stuff to end" > > Even worse: > > s = '' > while condition: > s = "insert stuff at beginning" + s > > because that defeats the runtime optimization (CPython only!) that > *sometimes* can alleviate the badness of repeated string concatenation. > > See Joel on Software for more: > > http://www.joelonsoftware.com/articles/fog0000000319.html > > But a single concatenation is more or less equally efficient as string > formatting operations (and probably more efficient, because you don't > have the overheard of parsing the format mini-language). > > For repeated concatenation, the usual idiom is to collect all the > substrings in a list, then join them all at once at the end: > > pieces = [] > while condition: > pieces.append('append stuff at end') > s = ''.join(pieces) > > > > > -- > Steven Thanks Steven, I was about to ask for an efficient way to concatenate an arbitrary amount of strings.