Reputation: 3507
I have the following python code :
#This program converts the speeds 60 KPH
#through 130 KPH (in 10 kph increments)
#to MPH
#Global constants
START = 60
END = 131
INCREMENT = 10
CONVERSION_FACTOR = 0.6214
def main():
#Print the table headings
print('KPH\t\tMPH')
print('----------------')
#Print the speeds
for kph in range(START, END, INCREMENT):
mph = kph * CONVERSION_FACTOR
print(kph, '\t\t', format(mph, '.1f'))
#Call the main function
main()
Running this code I get the following result :
KPH MPH
----------------
60 37.3
70 43.5
80 49.7
90 55.9
100 62.1
110 68.4
120 74.6
130 80.8
How can I right align the second column, so that my results are shown more properly?
Upvotes: 2
Views: 7812
Reputation:
Use the Format Specification Mini-Language
"{:>10.3}".format(12.34)
Result (using _
for spaces ):
______12.3
Upvotes: 12
Reputation: 6175
You could use printf
style formating to specify width too.
>>> print('%10.2f' % 1.23456)
1.12
In your example you could use:
print('%-10i%.1f' % (kph, mph))
Upvotes: 3