Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!goblin1!goblin2!goblin.stu.neva.ru!newsfeed.xs4all.nl!newsfeed5.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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'subject:: [': 0.03; 'string.': 0.04; 'formatting': 0.07; '%s"': 0.09; 'allocates': 0.09; 'subject:Tutor': 0.09; 'pm,': 0.11; 'this:': 0.11; 'am,': 0.14; 'received:74.125.82.44': 0.14; 'received:mail- ww0-f44.google.com': 0.14; 'wrote:': 0.14; '"%s': 0.16; '10:17': 0.16; 'allocate': 0.16; 'mart\xednez': 0.16; 'practice?': 0.16; 'specifier': 0.16; 'intermediate': 0.16; 'subject:] ': 0.16; 'tue,': 0.20; 'code': 0.22; 'header:In-Reply-To:1': 0.22; 'mon,': 0.22; 'memory': 0.24; 'url:mailman': 0.27; 'message- id:@mail.gmail.com': 0.28; 'hall': 0.29; 'string': 0.29; "python's": 0.29; 'solved': 0.31; 'strings,': 0.31; 'strings.': 0.31; 'subject:working': 0.31; 'to:addr:python-list': 0.32; 'url:listinfo': 0.33; 'normally': 0.35; 'considered': 0.36; 'two': 0.37; 'url:python': 0.37; 'subject:with': 0.37; 'apr': 0.38; 'faster': 0.38; 'strings': 0.38; 'received:google.com': 0.38; 'but': 0.38; 'code:': 0.38; 'url:org': 0.38; 'end': 0.39; 'to:addr:python.org': 0.39; 'how': 0.39; "it's": 0.40; '2011': 0.62; 'as:': 0.64; '10:34': 0.84; 'concatenate': 0.84; 'received:129': 0.84 MIME-Version: 1.0 In-Reply-To: <1303174733.4258.2.camel@localhost.localdomain> References: <1303174733.4258.2.camel@localhost.localdomain> Date: Mon, 18 Apr 2011 21:23:57 -0400 Subject: Re: [Tutor] working with strings in python3 From: Benjamin Kaplan To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Junkmail-Status: score=10/49, host=mpv1.tis.cwru.edu X-Junkmail-Signature-Raw: score=unknown, refid=str=0001.0A020206.4DACE42E.0078,ss=1,fgs=0, ip=74.125.82.44, so=2010-12-23 16:51:53, dmn=2009-09-10 00:05:08, mode=single engine X-Junkmail-IWF: false 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: 53 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1303176310 news.xs4all.nl 81482 [::ffff:82.94.164.166]:56294 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:3516 On Mon, Apr 18, 2011 at 8:58 PM, Westley Mart=EDnez wr= ote: > On Tue, 2011-04-19 at 10:34 +1000, James Mills wrote: >> On Tue, Apr 19, 2011 at 10:17 AM, Rance Hall wrote: >> > pseudo code: >> > >> > >> > message =3D "Bah." >> > >> > if test: >> > =A0 message =3D message + " Humbug!" >> > >> > print(message) >> > >> > end pseudo code >> >> Normally it's considered bad practise to concatenate strings. >> Use a a format specifier like this: >> >> > message =3D "Bah." >> > >> > if test: >> > =A0 message =3D "%s %s" (message, " Humbug!") >> > >> > print(message) >> >> Python3 (afaik) also introduced the .format(...) method on strings. >> >> cheers >> James >> >> -- >> -- James Mills >> -- >> -- "Problems are solved by method" > > How is concatenating strings bad practice? =A0I use code such as: > > string =3D 'hello' > string +=3D ' children.' > > a lot. > Python's strings are immutable. So adding strings together has to allocate memory for each intermediate string. Not a big deal if you're just concatenating two strings, but if you have a whole bunch of long strings, it's much more efficient and much faster to use string formatting which just allocates memory for the final string and then puts everything in. > -- > http://mail.python.org/mailman/listinfo/python-list >