Often the string formatters in python are referred to as old style and new style. The old-style is '%' and .format is known as the new style.
python中的字符串格式化程序通常被稱為舊樣式和新樣式。 舊樣式為'%' ,. format被稱為新樣式。
Simple positional formatting is the most common use-case. This formatting is used if the order of the arguments is not likely to change and there are very few elements to be concatenated.
簡單的位置格式是最常見的用例。 如果參數的順序不太可能更改并且要連接的元素很少,則使用這種格式。
Example:
例:
# string concatenation using %
print('%s %s'%('one','two'))
# string concatenation using .format
print('{} {}'.format('one','two'))
Output
輸出量
one two
one two
With the new style formatting, it is possible to give placeholders an explicit positional index. This allows for rearranging the order of display without changing the arguments. This feature is not available in the old style.
使用新的樣式格式,可以為占位符提供顯式的位置索引。 這樣可以在不更改參數的情況下重新排列顯示順序。 舊功能不提供此功能。
Example:
例:
print('{0} {1}'.format('one','two'))
print('{1} {0}'.format('one','two'))
Output
輸出量
one two
two one
填充和對齊字符串 (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, possible to define that a value should be padded to a specific length.
默認情況下,將值格式化為僅占用表示內容所需的盡可能多的字符。 但是,可以定義應將值填充為特定長度。
Example:
例:
print('%10s'%('test'))
print('{:>10}'.format('test'))
Output
輸出量
test
test
截斷長弦 (Truncating long strings)
It is also possible to truncate overly long values to a specific number of characters. The number behind a . (dot) in the format specifies the precision of the output. For strings that means that the output is truncated to the specified length. In our example, this would be 3 characters.
也可以將過長的值截斷為特定數量的字符。 后面的數字。 格式中的(點)指定輸出的精度。 對于字符串,這意味著輸出將被截斷為指定的長度。 在我們的示例中,這將是3個字符。
Example:
例:
print('%.3s'%('includehelp',))
print('{:.3}'.format('includehelp'))
Output
輸出量
inc
inc
號碼 (Numbers)
Example:
例:
print('%d' %(10000))
print('{:d}' .format(10000))
Output
輸出量
10000
10000
參數化格式 (Parametrized formats)
New style formatting allows all of the components of the format to be specified dynamically using parametrization. Parametrized formats are nested expressions in braces that can appear anywhere in the parent format after the colon. Old style formatting also supports some parametrization but is much more limited. Namely, it only allows parametrization of the width and precision of the output.
新樣式格式允許使用參數化動態指定格式的所有組件。 參數化格式是括號中的嵌套表達式,可以在冒號之后的父格式中的任何位置出現。 舊樣式的格式也支持某些參數化,但局限性更大。 即,它僅允許對輸出的寬度和精度進行參數化。
Example:
例:
from datetime import datetime
dt = datetime(2019, 12, 19, 4, 5)
print('{:{dfmt} {tfmt}}'.format(dt, dfmt='%Y-%m-%d', tfmt='%H:%M'))
Output
輸出量
2019-12-19 04:05
自定義對象 (Custom Objects)
The datetime example works through the use of the __format__() magic method. However, one defines custom format handling in own objects by overriding this method. This gives complete control over the format syntax used.
datetime示例通過使用__format __()魔術方法來工作。 但是,通過覆蓋此方法,可以在自己的對象中定義自定義格式處理。 這樣可以完全控制所使用的格式語法。
Example:
例:
class Type2000(object):
def __format__(self, format):
if (format == 'test-format'):
return "This is format example for include help."
return 'Type 2000'
print('{:test-format}'.format(Type2000()))
Output
輸出量
This is format example for include help.
翻譯自: https://www.includehelp.com/python/string-formatting-vs-format.aspx