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

两盒软妹~` 提交于 2019-12-04 03:16:28

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)
'##------------------'

With format you can nest the replacements:

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

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

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')
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!