[Tutor] working with strings in python3

Dan Stromberg drsalists at gmail.com
Tue Apr 19 04:20:55 CEST 2011


On Mon, Apr 18, 2011 at 6:23 PM, Benjamin Kaplan
<benjamin.kaplan at case.edu> wrote:
> On Mon, Apr 18, 2011 at 8:58 PM, Westley Martínez <anikom15 at gmail.com> wrote:
>> On Tue, 2011-04-19 at 10:34 +1000, James Mills wrote:
>>> On Tue, Apr 19, 2011 at 10:17 AM, Rance Hall <ranceh at gmail.com> wrote:
>>> > pseudo code:
>>> >
>>> >
>>> > message = "Bah."
>>> >
>>> > if test:
>>> >   message = message + " Humbug!"
>>> >
>>> > print(message)
>>> >
>>> > end pseudo code
>>>
>>> Normally it's considered bad practise to concatenate strings.
>>> Use a a format specifier like this:
>>>
>>> > message = "Bah."
>>> >
>>> > if test:
>>> >   message = "%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?  I use code such as:
>>
>> string = 'hello'
>> string += ' 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.

Agreed, adding two strings is not a big deal.  I think adding
1,000,000 strings Can be, but may not be.

ISTR that in some Python runtimes, adding n strings using + is O(n),
while in others it's O(n^2).  Needless to say, that's a pretty big
difference.

This is where the common ''.join(list_) stuff comes from - you put
your substrings in a list and join them with a separator of an empty
string.  This may actually be slower than repeated += in some
runtimes, but its guaranteed to be reasonable asymptotically speaking.

I actually find a + b a little more readable than '%s%s' % (a, b), but
the latter is less likely to need to be rearranged later.



More information about the Python-list mailing list