Newbie, homework help, please.

Roy Smith roy at panix.com
Sat Apr 21 16:28:44 EDT 2012


On 04/21/12 14:44, Roy Smith wrote:
> print  "* %-*s *" % (max_length, data)


On Apr 21, 2012, at 4:12 PM, Tim Chase wrote:
> Sounds like a lot more work and less flexible than using the (underemployed) .ljust() or .center() methods of a string. :-)
> 
>  print "* %s *" % data.ljust(42)


Six of one, half dozen of the other.  As I understand the problem, the width of the star-box is supposed to float to just enclose the longest line of data.  So, you still need to iterate over the lines to figure out which one is longest.  Once you've got that, it's the difference between:

> "* %-*s *" % (max_length, data)

> "* %s *" % data.ljust(max_length)

Use whichever one feels more natural to you.  I grew up with printf(), so I learned my way around all wonderful format specifiers.  If you don't come from that background, I guess I can see why it might all look like gibberish and ljust() comes to mind first.

--
Roy Smith
roy at panix.com




More information about the Python-list mailing list