FAQ

On Tue, Apr 19, 2011 at 10:17 AM, Rance Hall 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"

Search Discussions

  • Westley Martínez at Apr 19, 2011 at 12:58 am

    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 = "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.
  • Benjamin Kaplan at Apr 19, 2011 at 1:23 am

    On Mon, Apr 18, 2011 at 8:58 PM, Westley Mart?nez wrote:
    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 = "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.
  • Westley Martínez at Apr 19, 2011 at 2:09 am

    On Mon, 2011-04-18 at 21:23 -0400, Benjamin Kaplan wrote:
    message = "%s %s" % (message, " Humbug!")
    fix'd
  • Dan Stromberg at Apr 19, 2011 at 2:20 am

    On Mon, Apr 18, 2011 at 6:23 PM, Benjamin Kaplan wrote:
    On Mon, Apr 18, 2011 at 8:58 PM, Westley Mart?nez wrote:
    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 = "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.
  • Steven D'Aprano at Apr 19, 2011 at 2:16 am

    On Tue, 19 Apr 2011 10:34:27 +1000, James Mills wrote:

    Normally it's considered bad practise to concatenate strings.
    *Repeatedly*.

    There's nothing wrong with concatenating (say) two or three strings.
    What's a bad idea is something like:


    s = ''
    while condition:
    s += "append stuff to end"

    Even worse:

    s = ''
    while condition:
    s = "insert stuff at beginning" + s

    because that defeats the runtime optimization (CPython only!) that
    *sometimes* can alleviate the badness of repeated string concatenation.

    See Joel on Software for more:

    http://www.joelonsoftware.com/articles/fog0000000319.html

    But a single concatenation is more or less equally efficient as string
    formatting operations (and probably more efficient, because you don't
    have the overheard of parsing the format mini-language).

    For repeated concatenation, the usual idiom is to collect all the
    substrings in a list, then join them all at once at the end:

    pieces = []
    while condition:
    pieces.append('append stuff at end')
    s = ''.join(pieces)




    --
    Steven
  • Chris Angelico at Apr 19, 2011 at 3:22 am

    On Tue, Apr 19, 2011 at 12:16 PM, Steven D'Aprano wrote:
    See Joel on Software for more:

    http://www.joelonsoftware.com/articles/fog0000000319.html
    The bulk of that article is reasonable; he's right in that a good
    programmer MUST have at least some understanding of what's happening
    on the lowest level. He seems to consider C strings to be
    fundamentally bad, though; which isn't quite fair. See, a C-style
    ASCIIZ string can scale up to infinity - the Pascal strings he
    mentions are limited to 255 bytes, and while a forward-thinker might
    have gone as far as a 32-bit length (not guaranteed, and quite
    wasteful if you have billions of short strings - imagine if your
    32-bit arithmetic functions are double effort for the CPU), in today's
    world it's not that uncommon to work with 4GB or more of data. ASCIIZ
    may not be the most efficient for strcatting onto, but you shouldn't
    strcat in a loop like that anyway; rather than the mystrcat that he
    offered, it's better to have a mystrcpy (called strmov in several
    libraries) that's identical to strcpy but returns the end of the
    string. Identical to his version but without the dest++ scan first,
    and used in the same way but without bothering to put the starting \0
    in the buffer.

    Ultimately, though, every method of joining strings is going to have
    to deal with the "first strlen, then strcpy" issue. I haven't looked
    at Python's guts, but I would expect that list joining does this; and
    one of the simplest ways to code a StringBuffer object (which I've
    used on occasion in raw C) is to simply save all the pointers and then
    build the string at the end, which is really the same as
    "".join(list). (And yes, I know this depends on the memory still being
    allocated. I knew what I was doing when I took that shortcut.)

    "Anyway. Life just gets messier and messier down here in byte-land.
    Aren't you glad you don't have to write in C anymore?"

    Nope. I'm glad that I *can* write in C still. Well, actually I use C++
    because I prefer the syntax, but there are plenty of times when I want
    that down-on-the-metal coding.

    Oh, and by the way. XML sucks if you want performance... and it's so
    easy to abuse that I don't really see that it has much value for "data
    structures" outside of file transfers. You package your data up in
    XML, send it to the other end, they unpack it and turn it into what
    they want. End of XMLness. And if you want anything binary ("hey guys,
    here's the icon that I want you to display with this thing", for
    instance), it gets messier. Much neater to avoid it altogether.

    Chris Angelico
  • Westley Martínez at Apr 19, 2011 at 3:26 am

    On Tue, 2011-04-19 at 02:16 +0000, Steven D'Aprano wrote:
    On Tue, 19 Apr 2011 10:34:27 +1000, James Mills wrote:

    Normally it's considered bad practise to concatenate strings.
    *Repeatedly*.

    There's nothing wrong with concatenating (say) two or three strings.
    What's a bad idea is something like:


    s = ''
    while condition:
    s += "append stuff to end"

    Even worse:

    s = ''
    while condition:
    s = "insert stuff at beginning" + s

    because that defeats the runtime optimization (CPython only!) that
    *sometimes* can alleviate the badness of repeated string concatenation.

    See Joel on Software for more:

    http://www.joelonsoftware.com/articles/fog0000000319.html

    But a single concatenation is more or less equally efficient as string
    formatting operations (and probably more efficient, because you don't
    have the overheard of parsing the format mini-language).

    For repeated concatenation, the usual idiom is to collect all the
    substrings in a list, then join them all at once at the end:

    pieces = []
    while condition:
    pieces.append('append stuff at end')
    s = ''.join(pieces)




    --
    Steven
    Thanks Steven, I was about to ask for an efficient way to concatenate an
    arbitrary amount of strings.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedApr 19, '11 at 12:34a
activeApr 19, '11 at 3:26a
posts8
users6
websitepython.org

People

Translate

site design / logo © 2022 Grokbase