Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: [Tutor] working with strings in python3

Reply
Thread Tools

Re: [Tutor] working with strings in python3

 
 
James Mills
Guest
Posts: n/a
 
      04-19-2011
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"
 
Reply With Quote
 
 
 
 
Steven D'Aprano
Guest
Posts: n/a
 
      04-19-2011
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/articl...000000319.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
 
Reply With Quote
 
 
 
 
Chris Angelico
Guest
Posts: n/a
 
      04-19-2011
On Tue, Apr 19, 2011 at 12:16 PM, Steven D'Aprano
<steve+> wrote:
> See Joel on Software for more:
>
> http://www.joelonsoftware.com/articl...000000319.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
 
Reply With Quote
 
Westley Martínez
Guest
Posts: n/a
 
      04-19-2011
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/articl...000000319.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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: is the same betweent python3 and python3.2? Andrew Berg Python 0 06-16-2012 11:11 AM
python3 raw strings and \u escapes rurpy@yahoo.com Python 10 06-16-2012 02:14 AM
Could anyone give a working example using asynchat in Python3.0? davy zhang Python 0 10-22-2008 02:29 AM
Strings, Strings and Damned Strings Ben C Programming 14 06-24-2006 05:09 AM
Catching std::strings and c-style strings at once Kurt Krueckeberg C++ 2 11-17-2004 03:53 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57