Re: [Tutor] working with strings in python3

Discussion in 'Python' started by James Mills, Apr 19, 2011.

  1. James Mills

    James Mills Guest

    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"
    James Mills, Apr 19, 2011
    #1
    1. Advertising

  2. 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
    Steven D'Aprano, Apr 19, 2011
    #2
    1. Advertising

  3. 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
    Chris Angelico, Apr 19, 2011
    #3
  4. 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.
    Westley Martínez, Apr 19, 2011
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. willie

    Help ! I need a Java tutor!

    willie, Nov 12, 2003, in forum: Java
    Replies:
    3
    Views:
    1,054
    bharatchhajer
    Aug 19, 2006
  2. Tom
    Replies:
    4
    Views:
    500
    Tony Morris
    Jan 20, 2004
  3. Ben

    Strings, Strings and Damned Strings

    Ben, Jun 22, 2006, in forum: C Programming
    Replies:
    14
    Views:
    755
    Malcolm
    Jun 24, 2006
  4. Replies:
    10
    Views:
    711
    Jason Friedman
    Jun 16, 2012
  5. Andrew Berg
    Replies:
    0
    Views:
    330
    Andrew Berg
    Jun 16, 2012
Loading...

Share This Page