How do I left-align numbers in columns in Python?
I am trying to write a function that passes this doctest:
Prints a table of values for i to the power of 1,2,5 and 10 from 1 to 10
left aligned in columns of width 4, 5, 8 and 13
>>> print_function_table()
i i*2 i*5 i*10
1 1 1 1
2 4 32 1024
3 9 243 59049
4 16 1024 1048576
5 25 3125 976562开发者_如何学编程5
6 36 7776 60466176
7 49 16807 282475249
8 64 32768 1073741824
9 81 59049 3486784401
10 100 100000 10000000000
I feel like I am almost there but I can't seem to left-align my columns.
the code i have is:
def print_function_table():
i = 1
s = "{:^4} {:^5} {:^8} {:^13}"
print s.format("i", "i*2", "i*5", "i*10")
while i <= 10:
print s.format(i, i*2, i*5, i*10)
i += 1
What about...
def print_function_table():
s = "{:<4} {:<5} {:<8} {:<13}"
print s.format("i", "i*2", "i*5", "i*10")
for i in range(1,11):
print s.format(i, i**2, i**5, i**10)
?
精彩评论