Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #9096
| References | <4E17661D.6020307@gmail.com> <CAMuTYXjFu=1JgFYtuJAh_3UYce0WjRW-KH0EjEeYBtOx5AY6FA@mail.gmail.com> |
|---|---|
| From | Ian Kelly <ian.g.kelly@gmail.com> |
| Date | 2011-07-08 15:38 -0600 |
| Subject | Re: String concatenation vs. string formatting |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.787.1310161129.1164.python-list@python.org> (permalink) |
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