9

New formatting lets us do this: '{:.<12}'.format('##') - optional fill character.
Can we do that using old formatting?
(I know we can fill with spaces '%-12s' % '##' )

Also, old formatting lets us do this: '%-*s' % (12, '##') - variable length.
Can we do that using new formatting?

3 Answers 3

7

For doing variable length using new-format , you can use nesting of replacements -

>>> '{:{}<{}}'.format('##','.',12)
'##..........'
>>> '{:{}<{}}'.format('##','-',12)
'##----------'
>>> '{:{}<{}}'.format('##','-',20)
'##------------------'

Even spaces as fill character -

>>> '{:{}<{}}'.format('##',' ',20)
'##                  '

Please note you do not always need to use nesting of replacements, you can directly specify them in the format as well -

>>> '{: <12}'.format('##')
'##          '

You can also specify the position of each argument to decide which argument goes where. Example -

>>> '{2:{0}<{1}}'.format('.',12,'##')
'##..........'
>>> '{0:{1}<{2}}'.format('##','-',20)
'##------------------'
Sign up to request clarification or add additional context in comments.

1 Comment

Hmm , Would like to hear what is wrong in my answer.
3

With format you can nest the replacements:

'{:.<{}}'.format('##',12)

So format is more powerful. Optional fill characters are not possible with %.

Comments

1

For your first part of the question, you can left align and use a space as the fill char using a width of 12:

'%-*s' % (12, '##') can be replaced with '{: <12}'.format('##').

For the second part no you cannot specify the fill character with old style formatting.

There is a nice site here that shows most of what you can and cannot do with old vs new, a snippet that covers Padding and aligning strings:

Padding and aligning strings

By default values are formatted to take up only as many characters as needed to represent the content. It is however also possible to define that a value should be padded to a specific length.

Unfortunately the default alignment differs between old and new style formatting. The old style defaults to right aligned while for new style it's left.

Align right:

Old '%10s' % ('test',) 
New '{:>10}'.format('test')

Align left:

Old

'%-10s' % ('test',)
New

'{:10}'.format('test')

By argument:

In the previous example, the value '10' is encoded as part of the format string. However, it is possible to also supply such values as an argument.

Old

'%*s' % ((- 8), 'test')
New

'{:<{}s}'.format('test', 8)

Again, new style formatting surpasses the old variant by providing more control over how values are padded and aligned. You are able to choose the padding character:

This operation is not available with old-style formatting.

New

'{:_<10}'.format('test')
Output

And also center align values:

This operation is not available with old-style formatting.

New

'{:^10}'.format('test')

2 Comments

For the first part, idea was to have variable length. Nested formating solved that. Thanks for the pyformat link!
no prob, then nesting the {} will do the job '{: <{}}'.format('##',var) or '{: <{var}}'.format('##,var=var')

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.