#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    245
    Rep Power
    11

    How do I print using format that padded space between printed value ?


    root:/home/python_code# ./range_and_len.py

    I would like to see the column line up straight regardless
    of how many characters list[i] has

    Code:
    =============================
    i subscript,   list[i],     len(list[i]),     len(list)
    
    0              cat                3            3
    1              window                6            3
    2              idiosyncrasy                12            3
    
    
    #!/usr/bin/python
    
    import re
    import os
    import os.path
    import sys
    import string
    
    list = ['cat', 'window', 'idiosyncrasy']
    
    print "====================\n"
    
    print "i subscript,   list[i],     len(list[i]),     len(list)\n"
    
    for i in range(len(list)):
       print i, " "*12, list[i], " "*14, len(list[i]), " "*10, len(list)
    Last edited by linh; June 17th, 2004 at 06:00 PM.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    There are two alternatives:

    1) use the string ljust, rjust or center methods to pad the strings to the requires length.

    2) format using the % operator with the minimum width and precision set to the same values, e.g. "%10.10s" % var.

    I am in a hurry so can't provide a full example - RTFM.

    Dave - The Developers' Coach
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    245
    Rep Power
    11

    Smile reply


    Hi DevCoach and thank you for your help. I did tried the solution as you have suggested, but it still not line up when printed.

    print i, " "*12, list[i], "%10.10s" % len(list[i]), " "*10, len(list)
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    74
    Rep Power
    11
    Hi,

    I am sure there must be a much better way to do this, but i just wanted to try it this way.

    Code:
    for i in range(len(list)):
    	space = len(list[i])
    	if space > 9:
    		tab = 14
    	else:
    		tab = 15
    	
    	print i, " "*12, list[i], " "*(12-space), len(list[i]), " "*tab, len(list)
    Random
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    245
    Rep Power
    11

    Smile reply


    Hi Random. Thank you for your help.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2001
    Location
    Houston, TX
    Posts
    383
    Rep Power
    13
    Code:
    >>> L = ['cat', 'window', 'idiosyncrasy']
    >>> for n, item in enumerate(L):
    	print "%12s%14s%10s%10s" % (n, item, len(item), len(L))
    
    	
               0           cat         3         3
               1        window         6         3
               2  idiosyncrasy        12         3
    And if you need it left justified, you can use the ljust string method.
    Code:
    >>> for n, item in enumerate(L):
    	print "%s%s%s%s" % (str(n).ljust(12), str(item).ljust(14), str(len(item)).ljust(10), str(len(L)).ljust(10))
    
    	
    0           cat           3         3         
    1           window        6         3         
    2           idiosyncrasy  12        3
    Debian - because life's too short for worrying.
    Best. (Python.) IRC bot. ever.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    245
    Rep Power
    11

    Smile Strike


    Hi Strike. I ran the code, but it gives me an error.
    NameError: name 'enumerate' is not defined


    Code:
    L = ['cat', 'window', 'idiosyncrasy']
    for n, item in enumerate(L):
      print "%12s%14s%10s%10s" % (n, item, len(item), len(L))
    
    
    NameError: name 'enumerate' is not defined
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    Hi Strike. I ran the code, but it gives me an error.
    NameError: name 'enumerate' is not defined
    The enumerate built-in function was added in Python 2.3. I suggest you upgrade if you can.

    Dave - The Developers' Coach
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    Originally Posted by Strike
    And if you need it left justified, you can use the ljust string method.
    Code:
    >>> for n, item in enumerate(L):
    	print "%s%s%s%s" % (str(n).ljust(12), str(item).ljust(14), str(len(item)).ljust(10), str(len(L)).ljust(10))
    
    	
    0           cat           3         3         
    1           window        6         3         
    2           idiosyncrasy  12        3
    A better way is to use the '-' flag in the % operator:
    Code:
    >>> for n, item in enumerate(L):
    ... 	print "%-12s%-14s%-10s%-10s" % (n, item, len(item), len(L))
    ... 	
    0           cat           3         3         
    1           window        6         3         
    2           idiosyncrasy  12        3
    Dave - The Developers' Coach

IMN logo majestic logo threadwatch logo seochat tools logo