LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How do I print using format that padded space between printed value ? (https://www.linuxquestions.org/questions/programming-9/how-do-i-print-using-format-that-padded-space-between-printed-value-194984/)

Linh 06-18-2004 10:05 AM

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)


leonscape 06-18-2004 11:14 AM

I don't know python so my syntax is probably wrong but something like

Code:

offset1 = 14 - len(list[i])
if offset1 > 9
  offset2 = 9
else
  offset2 = 10
endif

print i, " "*12, list[i], " "*offset1, len(list[i]), " "*offset2, len(list)


Linh 06-18-2004 03:22 PM

reply
 
Thank you leonscape for your suggestion


All times are GMT -5. The time now is 05:08 PM.