Reputation: 167
How can I automatically format a string in Python?
I'm trying to print the remainders of pascal's triangle modulo seven, up to a row number specified by the user. Furthermore, I want it to be properly centered. For example, if the number of rows to be printed is 20, then I would print
print('{:^41}'.format(row_x))
For each row x<=20.
But, like I said above, I want the number of rows to change. I've tried
print('{:^'+str(rownumber*2 +1)'+'}'.format(row_x))
As well as
print('{:^rownumber*2 +1}'.format(row_x))
Both of which give me an error. What's the proper way of doing this?
Upvotes: 0
Views: 1177
Reputation: 2062
Doesn't work if inserted into the formatting area for some reason so use instead:
>>> x = 20
>>> a = '{:^' + str(2*x+1) + '}'
>>> print(a.format(x))
>>> 20
Upvotes: 1