Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python string formatting - old `%` vs new `str.format`

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?

like image 272
avamsi Avatar asked Aug 16 '15 19:08

avamsi


3 Answers

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)
'##------------------'
like image 139
Anand S Kumar Avatar answered Oct 14 '22 07:10

Anand S Kumar


With format you can nest the replacements:

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

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

like image 42
Daniel Avatar answered Oct 14 '22 08:10

Daniel


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')
like image 38
Padraic Cunningham Avatar answered Oct 14 '22 06:10

Padraic Cunningham