Instead of tabs, it is better to use line formatting directives, where you can specify the exact number of spaces and reserve for output.
for instance
print '>>%20s<<' %('some string') >> some string<<
will reserve 20 spaces and align this line for display. You can left-justify it using %-20s
print '>>%-20s<<' %('some string') >>some string <<
Numbers can be specified using d
for integers and f
for float values. For instance.
print '%03d %5.2f' %(5, 22/7.0) 005 3.14
In this example, I reserved 3 null spaces for an integer value and 5 spaces for float (2 for values ββafter decimal space). You can also control alignemnt with -
. There are many other options for formatting the output, see Suggested links below for more details.
Please note that using the .format()
function is .format()
. See SO my answer here for how to use .format()
. This answer may also help.
Although .format
may seem very verbose, it offers great flexibility, and one of my favorite features is the ability to easily print large numbers of commas for easy reading.
For more information, see Python's String Formatting Docs to help you build your output exactly as you would like.
Levon source share