[Tutor] First program

Ray Parrish crp at cmc.net
Sat Mar 13 18:55:19 CET 2010

Luke Paireepinart wrote:
> On Sat, Mar 13, 2010 at 3:03 AM, Alan Gauld <alan.gauld at btinternet.com 
> <mailto:alan.gauld at btinternet.com>> wrote:
>     "Ray Parrish" <crp at cmc.net <mailto:crp at cmc.net>> wrote
>                print "A %s with dimensions %sx%s has an area of %s." %
>             (choice, height, width, width*height)
>         Isn't it a little more understandable to use a construct like
>         the following?
>                     print "The area of a " + Choice + "is " str(Width)
>                     + " x " +
>         str(Height) + " equals " + str(Width * Height) + " square feet"
>     It depends on where you come from.
>     Those of us brought up on C or COBOL are used to separating the
>     presentation from the data. Those brought up with PASCAL and BASIC
>     are used to iterleaving data with presentation.
>     One thing - you don't need all the str() calls in your example,
>     print already calls str() for you. Also comma separators are
>     better than + signs since the plus operation on strings is quite
>     expensive - you create a new string for each addition.
> print actually doesn't call str if you use concatenation.  So the 
> str() calls are necessary if you do not use "," but use "+" instead. 
> So there are at least 2 reasons why + is worse than comma.
> Another thing to be aware of is that if you use commas,
> print inserts a space in the string, which may be either an advantage 
> or a disadvantage depending on what you're trying to do.
> -Luke
Ahhh, thank you for the clarifications. for the cases where spaces are 
needed it the commas seem like a pretty good way to do it, but when I 
need to add together stuff with no spaces I'll try the formatted method.

Thanks again, Ray Parrish

Linux dpkg Software Report script set..
Ray's Links, a variety of links to usefull things, and articles by Ray.
Writings of "The" Schizophrenic, what it's like to be a schizo, and other
things, including my poetry.

More information about the Tutor mailing list