|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
|||
|
|||
|
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 |
|
#3
|
|||
|
|||
|
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) |
|
#4
|
|||
|
|||
|
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 |
|
#5
|
|||
|
|||
|
Hi Random. Thank you for your help.
|
|
#6
|
|||
|
|||
|
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 |
|
#7
|
|||
|
|||
|
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 |
|
#8
|
|||
|
|||
|
Quote:
The enumerate built-in function was added in Python 2.3. I suggest you upgrade if you can. Dave - The Developers' Coach |
|
#9
|
|||
|
|||
|
Quote:
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 |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > How do I print using format that padded space ? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|