日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

Python字符串格式:%vs.format

發布時間:2023/12/1 python 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python字符串格式:%vs.format 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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

輸出量

testtest

截斷長弦 (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

總結

以上是生活随笔為你收集整理的Python字符串格式:%vs.format的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。