Ray, please reply on-list in the future in case someone else has input.<br><br><div class="gmail_quote">On Fri, Mar 12, 2010 at 8:01 PM, Ray Parrish <span dir="ltr"><<a href="mailto:crp@cmc.net">crp@cmc.net</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">Luke Paireepinart wrote:<div class="im"><br><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
print "A %s with dimensions %sx%s has an area of %s." % (choice, height, width, width*height)<br>
<br></blockquote></div><div class="im"><br>
Isn't it a little more understandable to use a construct like the following?<br>
<br>
>>> print "The area of a " + Choice + "is " str(Width) + " x " + str(Height) + " equals " + str(Width * Height) + " square feet"<br>
<br>
The area of a rectangle is 12 x 10 equals 120 square feet.<br>
<br>
I find that putting the variables on the end like that, when you're not actually applying any special formatting to them makes it less readable when I'm debugging my stuff, or when someone else is reading my code, and trying to understand it.<br>
<div class="h5"><br>
<br>
</div></div></blockquote><div>Your version creates at least 10 intermediate strings before outputting.<br>Remember strings are immutable in Python. <br>So you're constructing strings<br>The area of a<br>The area of a rectangle<br>
The area of a rectangle is<br>12<br>The area of a rectangle is 12<br>The area of a rectangle is 12 x <br>10<br>The area of a rectangle is 12 x 10<br>The area of a rectangle is 12 x 10 equals<br>120<br>
The area of a rectangle is 12 x 10 equals 120<br>
The area of a rectangle is 12 x 10 equals 120 square feet<br><br>With string formatting you avoid all of these intermediate strings, so it's arguably more efficient.<br>Other than just viewing from a performance standpoint though, I find it much easier to read my version, because any computation required takes place at the end of the line.<br>
For example, your inline str(width*height) requires you to read the whole line to see it.<br><br>It's really a personal thing, it's easier for me to read the formatting version than the string concatenation version, in most cases.<br>
Now if you had used the comma convention I would have seen your point. This is, I think, the easiest to read of all 3<br>area = width * height<br>print "The area of a", choice, "is", width, "x", height, ", which equals", area, "square feet."<br>
<br>Also, why are you capitalizing variable names? That's a pretty unusual convention.<br><br>-Luke<br><br><br> </div></div><br>