Python 語法淺析:字符串格式化

前序

There should be one - and preferably only one - obvious way to do it.
———— the Zen of Python
意譯:Python提倡用一種,並且最好是隻有一種方法來完成一件事python

雖然 Python 有以上的提倡,但卻在字符串格式化方面,沒有作到這一點。bash

字符串格式化

敲黑板,劃重點:在 Python 中有至少三種常見方式實現字符串格式化:app

  1. %-formatting 格式(Python2.6之前,推薦輸出時使用)
  2. str.format() 格式(Python2.6,推薦字符串拼接使用)
  3. f-string 格式(Python3.6,推薦使用)

一、printf 風格的字符串格式化(%-formatting 格式)

Python2.6 以前只有這一種方式,使用與 C 中 printf 函數同樣的語法。函數

基礎語法:format % value(其中 format 爲一個字符串),在 format 中的 % 轉換標記符將被替換爲零個或者多個 value 條目性能

基礎用法

# coding in Python3.7
print('this is %s blog written in %d%%%02d%%%d %02d:%d:%06.2f' 
      % ('TinyDolphin', 2019, 5, 31, 9, 30, 22.3333))
# this is TinyDolphin blog written in 2019%05%31 09:30:022.33

print('title=%(title)s, author=%(name)s' 
      % {'name': 'TinyDolphin', 'title': 'Python 語法淺析:字符串格式化'})
# title=Python 語法淺析:字符串格式化, author=TinyDolphin

print('%0*.*f' % (6, 2, 2.345))
# 002.35
複製代碼

printf 語法

針對這種格式化操做,重要知識點以下:
一、針對這些轉換標記字符,必須按照如下順序:
    % --> (name) --> '#''-''0''+'' ' --> m.n | m --> d、s、r、f

二、經常使用轉換類型:
    %s 格式化字符串(str()函數)
    %r 格式化字符串(repr()函數)
    %d 格式化整數
    %f 格式化浮點數字,可指定小數點後的精度

三、經常使用的轉換標記字符:
    - 用作左對齊
    + 在正數前面顯示加號( + )
    # 在八進制數前面顯示零('0'),在十六進制前面顯示'0x'或者'0X'(取決於用的是'x'仍是'X')
    0 顯示的數字前面填充'0',而不是默認的空格
    % '%%'輸出一個單一的'%'
    m.n 表示 m 是顯示的最小總寬度,n 是小數點後的位數
    * 定義寬度或者小數點精度(用在不能預先指定m.n的值)
    (var) 映射變量(字典參數)
複製代碼

二、字符串的方式(str.format()格式)

Python2.6 開始,新增了一種格式化字符串的函數 str.format(),它加強了字符串格式化的功能,好比:支持位置映射關鍵字映射對象屬性映射下標映射等多種方式this

基本語法是經過 {} 和 : 來代替之前的 %,例如:'%03.2f'被改寫成 '{:03.2f}'。spa

兩個格式化方法:code

str.format(*args, **kwargs)
    字符串的格式化操做。
    str 包含字符串字面值 AND {} 括起來的替換域。
    每一個替換域:位置參數的索引 OR 關鍵字參數的名稱
    
str.format_map(mapping)
    相似於 str.foramt(**mapping)
    不一樣之處:mapping 會被直接使用而不是複製到一個dict。
複製代碼

PS:Python 存在內置函數 format(value, format_spec):會轉換成 type(value).format(value, format_spec)orm

基礎用法

一、按位置訪問參數對象

'{0}-{1}-{2}'.format('a', 'b', 'c')     # 'a-b-c'
'{}-{}-{}'.format('a', 'b', 'c')        # 'a-b-c'
'{2}-{1}-{0}'.format('a', 'b', 'c')     # 'c-b-a'
'{2}-{1}-{0}'.format(*'abc')            # 'c-b-a'

args = ['a', 'b', 'c']
'{2}-{1}-{0}'.format(*args)             # 'c-b-a'

'{0}-{1}-{0}'.format(*['abc', 'def'])   # 'abc-def-abc'
複製代碼

二、按名稱訪問參數

'{a}-{b}'.format(a='1', b='2')          # '1-2'

kwargs = {'a':'1', 'b':'2'}
'{a}-{b}'.format(**kwargs)              # '1-2'
複製代碼

三、訪問參數的屬性

class Point:
    def __init__(self, x, y):
        self.x, self.y = x, y
    def __str__(self):
        return 'Point({self.x}, {self.y})'.format(self=self)
        
str(Point(3, 4))                        # 'Point(3, 4)'
複製代碼

四、訪問參數的項

point = (3, 5)
'X:{0[0]} Y:{0[1]}'.format(point)       # 'X:3 Y:5'
複製代碼

五、替代 %s 和 %r (!s、!r)

'str() = {!s}; repr() = {!r}'.format('a', 'a')  # "str() = a; repr() = 'a'"
複製代碼

六、對齊文本以及制定寬度(:<、:^、:>)

'{:<20}'.format('left aligned')         # 'left aligned '
'{:>20}'.format('right aligned')        # ' right aligned'
'{:^20}'.format('centered')             # ' centered '
# 使用 '*' 做爲填充字符
'{:*^20}'.format('centered')            # '******centered******'
複製代碼

七、替代 %+f、%-f 和 %f 以及指定正負號(:+、:-、:)

'{:+f} {:+f}'.format(3.14, -3.14)       # '+3.140000 -3.140000'
'{: f} {: f}'.format(3.14, -3.14)       # ' 3.140000 -3.140000' 正號—>空格
'{:-f} {:-f}'.format(3.14, -3.14)       # '3.140000 -3.140000' == '{:f} {:f}'
複製代碼

八、替換 %x 和 %o 以及轉換基於不一樣進位制的值(:x、:d、:o、:b)

'int:{0:d} hex:{0:x} oct:{0:o} bin:{0:b}'.format(42)
# 'int:42 hex:2a oct:52 bin:101010'
'int:{0:d} hex:{0:#x} oct:{0:#o} bin:{0:#b}'.format(42)
# 'int:42 hex:0x2a oct:0o52 bin:0b101010'
# '#' : 加上前綴:0x、0o、0b
複製代碼

九、使用逗號做爲千位分隔符(:,)

'{:,}'.format(123456789)                # '123,456,789'
複製代碼

十、表示爲百分數(:.2%)

'{:.2%}'.format(19/22)                  # '86.36%'
複製代碼

十一、使用特定類型的專屬格式化

import datetime
d = datetime.datetime(2019, 6, 10, 22, 5, 13)
'{:%Y-%m-%d %H:%M:%S}'.format(d)
# '2019-06-10 22:05:13'
複製代碼

十二、嵌套參數以及更復雜的示例

複製代碼

三、格式化字符串字面值(f-string 格式)

是帶有 'f' 或 'F' 前綴的字符串字面值。

花括號之外的部分按其字面值處理,除了雙重花括號 '{{' 或 '}}' 會被替換爲相應的單個花括號。

語法以下:

單個花括號 '{',標示一個替換字段(以 Python 表達式打頭)
 + 可能有一個以歎號 '!' 標示的轉換字符 
 + 可能有一個以冒號 ':' 標示的格式說明符 
 + 以一個右花括號 '}' 做爲結束
複製代碼

注意:格式化字符串字面值中的表達式:

  • 看成正常的 Python 表達式處理這一點很重要,也很強大
  • 不容許空表達式
  • lambda 表達式必須顯式地加上圓括號
  • 能夠包含換行(例如三引號字符串中)
  • 不能包含註釋
  • 從左到右被求值

若是指定了轉換符,則表達式求值的結果會先轉換再格式化。以後使用 Python 內置函數 format() 進行格式化。

基本用法

name = 'tinyDolphin'
f'my name is {name}'            # 'my name is tinyDolphin'
f'my name is {name!r}'          # "my name is 'tinyDolphin'"

width = 10
precision = 4
value = 12.34567
f'{value:{width}.{precision}}'  # '     12.35'

today = datetime.datetime(year=2019, month=6, day=10)
f'{today:%B %d, %Y}'            # 'June 10, 2019'

number = 1024
f'{number:#0x}'                 # '0x400'
複製代碼

PS:與 str.format() 有一點不一樣:在 str.format() 中,非數字索引將自動轉化爲字符串,而f-strings則不會

kwargs = {'a':1, 'b':2}
# 使用 str.format()
'a={kwargs[a]}'.format(kwargs=kwargs)   # 'a=1'

# 使用 f-string
f"a={kwargs[a]}"                        # × 發生異常
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'a' is not defined

# 正確使用 f-string
f"a={kwargs['a']}"                      # 'a=1'

複製代碼

總結

從如下三個方面進行對比:

速度上:f-string > %-formatting > str.format()
功能上:f-string > str.format() > %-formatting
可讀性: f-string > str.format() > %-formatting
複製代碼

對於速度驗證,筆者就不在此進行驗證了。

推薦使用場景:

%-formatting:Python2 中,因爲其性能優點,在涉及輸出的一些操做時,優先推薦使用
str.format():Python2 中,因爲其功能優點,在涉及字符串拼接的一些操做時,優先推薦使用
f-string:    Python3 中,強烈推薦使用
複製代碼
相關文章
相關標籤/搜索