Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #9096
| 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 | <ian.g.kelly@gmail.com> |
| 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 | <CAMuTYXjFu=1JgFYtuJAh_3UYce0WjRW-KH0EjEeYBtOx5AY6FA@mail.gmail.com> |
| References | <4E17661D.6020307@gmail.com> <CAMuTYXjFu=1JgFYtuJAh_3UYce0WjRW-KH0EjEeYBtOx5AY6FA@mail.gmail.com> |
| From | Ian Kelly <ian.g.kelly@gmail.com> |
| 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 <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.787.1310161129.1164.python-list@python.org> (permalink) |
| 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 |
Show key headers only | View raw
On Fri, Jul 8, 2011 at 3:23 PM, Benjamin Kaplan <benjamin.kaplan@case.edu> wrote: > String formatting is the One Right Way here. It's fine to use string > concatenation for a few things, but the operation is O(n^2) because each > concat occurs one at a time: Python allocates space for a string the size of > the first 2 things, copies the contents over. Then allocate a string the > 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 With string > formatting, Python creates a single string large enough to copy all the > formatting arguements in and then copies the 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 and > the plusses) and it's better for dealing with internationalization if you > 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.
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: String concatenation vs. string formatting Ian Kelly <ian.g.kelly@gmail.com> - 2011-07-08 15:38 -0600
csiph-web