#1
  1. PHP ninja
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Melbourne, Australia
    Posts
    20
    Rep Power
    0

    Formatting output


    Is there anyway to neatly for mat output in python so that it lines up in columns??

    Code:
    def displayUserDetails(myforum):
      "This function display the threads in the forum"
      list = user.listDetails()
      print "ID        Occupation        First Name     Last Name"
      for user in list:
        print "%s  %s     %s      %s"%(user[0],user[1],user[2],user[3])
    The problem i'm having is that the output doesn't line up neatly depending on the the length of the user vars??
  2. #2
  3. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    You can do a lot with pythons string formatting options, if all you want to do is justify left or right then you should only have to add a number (min lengh) in between the '%' and 's' and your away!

    check this page out of a little more detail:

    http://www.python.org/doc/current/li...q-strings.html

    Code:
    >>> #use '-' to justify left.
    >>> print '%10s %-7s %15s' % ('never', 'eva', 'ever')
    ...
    Try varying the lengh of the strings at the end and see what happens. Or fire up the Python shell and start playing .

    Hope this helps,

    Mark.
    Last edited by netytan; May 29th, 2004 at 01:38 PM.
    programming language development: www.netytan.com Hula


IMN logo majestic logo threadwatch logo seochat tools logo