[Tutor] Re: simple problem

Jeff Shannon jeff@ccvcorp.com
Fri, 05 Jul 2002 18:56:00 -0700


Derrick 'dman' Hudson wrote:

> | myname = 'Sean'
> | person = 'Kyle'
> |
> | a) print "Hello, " + person + " my name is " + myname
> |
> | b) print "Hello, %s my name is %s" % (person, myname)
>
>   Both a and b will print exactly what you wrote (with an
> extra newline at the end).

Be aware, though, that if you're doing *lots* of this (like, for instance,
adding numerous strings inside a tight loop or something), a is much less
efficient.  Each time you add a string, the interpreter creates a new string
object and copies the contents of the existing strings there.  In this
particular example, that's three temporary string objects, but if you're
building up a long string out of numerous variables, it could be a *lot*
more.  On the other hand, method b will only create a single string object, no
matter how many different variables you insert.  Also, b will let you do a lot
more specific alignment, formatting of numbers, etc.   There's really nothing
to recommend a as being better than b, either, so learn those string
formatting codes!  :)

Jeff Shannon
Technician/Programmer
Credit International





> c will add a space in between each
> section.  That is by design, just be aware of it.
>
> -D
>
> --
>
> A perverse man stirs up dissension,
> and a gossip separates close friends.
>         Proverbs 16:28
>
> http://dman.ddts.net/~dman/
>
>   ----------------------------------------------------------------------
>    Part 1.2Type: application/pgp-signature