I have a dictionary that looks something like this:
d = {
'last_price': '760.54',
'change': 'N/A',
'days_range': '760.00 - 775.00',
'previous_close': '771.23',
'change_percent': '-1.39%',
}
All of the values in this dictionary are subject to change. The numbers can vary widely from single digits to numbers several digits long. I would like to format this information and consistently present it like this:
Last: $760.54 Change: N/A (-1.39%)
Prev Close: $771.23 Day's Range: $760.00 - $775.00
So far, I've been trying to do this by doing this:
print "\
Last: {last_price:>17} \t Change: {change:>11} ({change_percent}) \n\
Prev Close: {previous_close:>11} \t Day's Range: {days_range:>18}\
".format(**d)
This produces output that looks like this:
Last: 760.54 Change: N/A (-1.39%)
Prev Close: 771.23 Day's Range: 760.00 - 775.00
While this works well enough if the numbers don't change, if "days_range" is set to a value like "78.91 - 79.91" it will jump further to the right. For example:
Last: 79.21 Change: N/A (-1.19%)
Prev Close: 80.16 Day's Range: 78.91 - 79.91
I also can't say I'm very content with using "\t" to separate the columns, as I am concerned that if the values in the first column are too long this can cause the right column to get pushed further right than desired.
Is there an easy and elegant way to do this? I would prefer a solution that does not depend on nonstandard libraries and is relatively straightforward.
I'd also like to know an easy way to prepend values with dollar signs without having to modify the values in the dictionary.