Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python string format width

I am having problems replicating the format as shown in:

desired output formatting

what my results are:

current output formatting

I'm currently using:

print '{}.{:<20} {}.'.format(i,'sum so far:',sum)

I have tried left, right, and center alignment, but I just can't get the format that I want.

like image 556
sutoL Avatar asked Feb 25 '26 22:02

sutoL


1 Answers

Convert the entire header portion, including the '.', to a string first, so you can calculate the width including it.

>>> '{:<20} sum so far: {}.'.format('{}.'.format(9), 123)
'9.                   sum so far: 123.'
>>> '{:<20} sum so far: {}.'.format('{}.'.format(10), 123)
'10.                  sum so far: 123.'
like image 101
o11c Avatar answered Feb 28 '26 11:02

o11c



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!