integer to ascii

Tim Roberts timr at probo.com
Thu Nov 8 00:30:50 EST 2001


Jeff Shannon <jeff at ccvcorp.com> wrote:
>
>Or:
>
>rows = 2
>columns = 3
>joined = "%d%d" % (rows,columns)
>
>String formatting is your friend.  It is *much* faster than concatenation with
>+, too (though this won't show up unless you're doing it in a tight loop...)

Yes.  This was one of the biggest surprises I had in coming to Python from
Perl.  I couldn't understand why I kept seeing

   this =  "%s and %s" % ("spam","eggs")

instead of

   this = "spam" + " and " + "eggs"

(OK, that's contrived, but you get the idea.)

It wasn't until someone pointed out that the former is one operation in C,
whereas the second is three interpreted operations, that I finally grokked
the idiom.
--
- Tim Roberts, timr at probo.com
  Providenza & Boekelheide, Inc.



More information about the Python-list mailing list