Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #9096 > unrolled thread
| Started by | Ian Kelly <ian.g.kelly@gmail.com> |
|---|---|
| First post | 2011-07-08 15:38 -0600 |
| Last post | 2011-07-08 15:38 -0600 |
| Articles | 1 — 1 participant |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: String concatenation vs. string formatting Ian Kelly <ian.g.kelly@gmail.com> - 2011-07-08 15:38 -0600
| From | Ian Kelly <ian.g.kelly@gmail.com> |
|---|---|
| Date | 2011-07-08 15:38 -0600 |
| Subject | Re: String concatenation vs. string formatting |
| Message-ID | <mailman.787.1310161129.1164.python-list@python.org> |
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 top | Article view | comp.lang.python
csiph-web