Skip to content

Instantly share code, notes, and snippets.

@griggheo
Created April 23, 2010 21:13
Show Gist options
  • Save griggheo/377190 to your computer and use it in GitHub Desktop.
Save griggheo/377190 to your computer and use it in GitHub Desktop.
$ cat templatize.py
def templatize(*args):
"""A small utility function for printing values in columns of fixed width
* the width is hardcoded as 20 in this case"""
format_string = ""
i = 0
for arg in args:
format_string += "{%d:>20}" % i
i += 1
print format_string.format(*args)
if __name__ == "__main__":
templatize("Column1","LongColumn","ThisIsBig","AlmostTwentyChars")
templatize("value 1",1,2,"value 4")
templatize(4,"value 122112",1,2)
$ # the format string used in this example is constructed as "{0:>20}{1:>20}{2:>20}{3:>20}"
$ # here's the output:
$ python templatize.py
Column1 LongColumn ThisIsBig AlmostTwentyChars
value 1 1 2 value 4
4 value 122112 1 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment