The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
How do I print using format that padded space ?
Discuss How do I print using format that padded space ? in the Python Programming forum on Dev Shed. How do I print using format that padded space ? Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

June 17th, 2004, 05:47 PM
|
|
Contributing User
|
|
Join Date: Jun 2003
Posts: 245
Time spent in forums: 11 m 27 sec
Reputation Power: 10
|
|
|
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.
|

June 18th, 2004, 02:41 AM
|
|
Contributing User
|
|
Join Date: Feb 2004
Location: London, England
|
|
|
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
|

June 18th, 2004, 12:06 PM
|
|
Contributing User
|
|
Join Date: Jun 2003
Posts: 245
Time spent in forums: 11 m 27 sec
Reputation Power: 10
|
|
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)
|

June 18th, 2004, 01:38 PM
|
|
Contributing User
|
|
Join Date: Aug 2003
Posts: 72
Time spent in forums: 2 h 47 m 23 sec
Reputation Power: 10
|
|
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
|

June 18th, 2004, 03:11 PM
|
|
Contributing User
|
|
Join Date: Jun 2003
Posts: 245
Time spent in forums: 11 m 27 sec
Reputation Power: 10
|
|
reply
Hi Random. Thank you for your help.
|

June 18th, 2004, 04:50 PM
|
|
Contributing User
|
|
Join Date: Dec 2001
Location: Houston, TX
Posts: 383
Time spent in forums: 1 h 41 m 27 sec
Reputation Power: 12
|
|
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
|

June 18th, 2004, 05:46 PM
|
|
Contributing User
|
|
Join Date: Jun 2003
Posts: 245
Time spent in forums: 11 m 27 sec
Reputation Power: 10
|
|
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
|

June 18th, 2004, 06:33 PM
|
|
Contributing User
|
|
Join Date: Feb 2004
Location: London, England
|
|
Quote: 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
|

June 18th, 2004, 06:38 PM
|
|
Contributing User
|
|
Join Date: Feb 2004
Location: London, England
|
|
Quote: | 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
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|