I might be misunderstanding you, but is this what you want?
>>> print '{:.2f} {:<20}'.format(1.0, 'kg/hr')
1.00_kg/hr_______________
Answer from TerryA on Stack OverflowI might be misunderstanding you, but is this what you want?
>>> print '{:.2f} {:<20}'.format(1.0, 'kg/hr')
1.00_kg/hr_______________
To solve the problem (in the comment above, to pass a single field) you may try:
d = {'value':1.0, 'unit':'kg/hr'}
print '{value:.2f} {unit:<20}'.format(**d)
where d may be a dictionary member of your class.
Also, I would just filter the value to accept only floats.
Left-justifying floating-point number output
python - align float and specify precision in string formatting - Stack Overflow
Python: Format string with float, right alignment - Stack Overflow
How do I right-justify strings with dollar signs and percentages?
How do you format a float to two decimal places in Python?
Is percent formatting deprecated in Python?
What is the best way to format strings in Python?
I have an intro Python assignment that asks me to list out a series of numbers with two decimal places and align all the decimal points.
Someone please tell me how I can line these numbers up. Formatting doesn't seem to do literally anything at all.
This shows an example of how to format your output with two decimal points using the older % formatting method:
v1 = 55.39
v2 = -1.15
v3 = 6.54
v4 = 0.00
print '%8.2f %8.2f' % (v1, v2)
print '%8.2f %8.2f' % (v3, v4)
the corresponding output:
55.39 -1.15
6.54 0.00
Alternatively, you can use the "new and improved" .format() function which will be around for a while and is worth getting to know. The following will generate the same output as above:
print '{:8.2f} {:8.2f}'.format(v1, v2)
print '{:8.2f} {:8.2f}'.format(v3, v4)
Both sets of formatting directives allocate 8 spaces for your number, format it as a float with 2 digits after the decimal point. You'd have to adjust these values to fit your needs.
Using this approach to format your output will be easier I think since .format() will give you a lot of control over the output (incl. justifying, filling, precision).
Python Format Specification Mini-Language
print(f'{symbol:<5}: {name:<44} {buyprices:>10.2f} change: {val:>5.2f}')
Output:
AXP : American Express Company 55.38 change: -1.15
AXR : Amrep Corp. 6.54 change: 0.00
How it works:
<44 (left justified, 44 places)
>5.2f (right justified, 5 places, 2 decimal places)
You could use:
print('{:<15}: {:>10.2f}'.format('Bonus', bonus),sep='')
The > takes care of right alignment while the .2f takes care of the decimal points.
Chaining string formatters works:
>>> "Cash Money: %10s" % "$%.2f" % 6.124
'Cash Money: $6.12'
The first one adds the symbol & formats the number, the second pads it to the proper length.
Used in a more realistic way:
str_bal = "$%.2f" % bal
print "Balance: %20s" % str_bal
A real world example:
I've build a report which intelligently resizes via mixing format strings:
format_string = "%-9s %-{}s %6.3f%% (%{}i/%{}i) total events: %{}i".format(
max_len_key, max_len_pos, max_len_total, max_len_events)
for entry in table:
...
print format_string % (title, key, percent, pos, total, event)
which outputs:
Version: 1.0.0.0 2.688% (10/372) total events: 150
Version: 1.4.1.15 0.000% ( 0/181) total events: 73
Note: you can do this without mixing formatters, but I think this ends up clearer than the abomination "%%-9s %%-%is %%6.3f%%%% (%%%ii/%%%ii) total events: %%%ii" (which is the valid replacement for the first format string).
