简体   繁体   English

Python字符串格式化 - 旧`%`vs new`str.format`

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

New formatting lets us do this: '{:.<12}'.format('##') - optional fill character. 新的格式允许我们这样做: '{:.<12}'.format('##') - 可选的填充字符。
Can we do that using old formatting? 我们可以使用旧格式吗?
(I know we can fill with spaces '%-12s' % '##' ) (我知道我们可以填充空格'%-12s' % '##'

Also, old formatting lets us do this: '%-*s' % (12, '##') - variable length. 此外,旧格式化允许我们这样做: '%-*s' % (12, '##') - 可变长度。
Can we do that using new formatting? 我们可以使用新的格式吗?

For doing variable length using new-format , you can use nesting of replacements - 为了使用new-format进行可变长度,您可以使用替换嵌套 -

>>> '{:{}<{}}'.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您可以嵌套替换:

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

So format is more powerful. 所以format更强大。 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: 对于问题的第一部分,您可以保持对齐并使用空格作为填充字符,宽度为12:

'%-*s' % (12, '##') can be replaced with '{: <12}'.format('##') . '%-*s' % (12, '##')可以替换为'{: <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: 有一个不错的网站, 在这里 ,显示大部分你能不能与旧VS新做的,覆盖填充和对齐字符串的一个片段:

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. 在前面的示例中,值“10”被编码为格式字符串的一部分。 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')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM