3

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.

2 Answers 2

2

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.'
Sign up to request clarification or add additional context in comments.

Comments

0

You can try first aligning the item number string:

tot = 0 
for i in xrange(1, 11):
    tot += i
    print '{:<20}{} {}.'.format(str(i) + '.',  'sum so far:', tot)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.