printf 風格 字符串格式化(printf-style String Formatting )
我們知道 printf風格字符串格式化 已經不被python官方推薦使用,但是爲什麼還需要學習它呢?在學習某些庫的源代碼時,因爲歷史原因,有些字符串的格式化處理還是該風格,所以爲了能讀懂源碼,還是需要了解它的使用方法。
語法
String Formatting語法:
format
%values
format
就是一個字符串,format中的% 轉換限定符(conversion specification)
將會被values
中的元素所替代。字符串對象有且僅有一個唯一的操作符-%,可稱爲 字符串格式化操作符(string formatting operator) 或 字符串插值操作符(string interpolation operator)。
說明: string formatting operator(%) 是指
format
和values
之間的%。
format
格式語法:
format
中的 轉換限定符(conversion specifier) 由兩個或多個字符 按相應的順序組成。
%
,標誌 着轉換限定符的開始;- 映射關鍵詞(Mapping key),可選項,由小括號括起來的字符序列;例如,(key_name);
- 轉換標誌(conversion flag),可選項,影響一些轉換類型(conversion type)的結果;
- 最小域寬(minimum field width),可選項,如果指定爲星號(*,asterisk),那麼讀取
values
元組的下一個元素的值作爲實際的寬度,而要轉換的對象在域寬和精度之後;即域寬可以在vaules
中指定- 精度(precision),可選項,精度的表示以字符’.'開始,後面的數字表示精確到第幾位,如果指定爲星號(*,asterisk),那麼讀取的
values
元組的下一個元素的值作爲實際的精度,而要轉換的對象在精度之後;即精度可以在vaules
中指定- 長度修飾符(length modifier),可選項;目前基本不用了,就不再做介紹
- 轉換類型;
conversion flag
Flag | Meaning |
---|---|
‘#’ | The value conversion will use the “alternate form” (where defined below). |
‘0’ | The conversion will be zero padded for numeric values.填充0 |
‘-’ | The converted value is left adjusted (overrides the ‘0’ conversion if both are given).轉換後的值向左調整,如果’0‘也存在將不起作用 |
’ ’ | (a space) A blank should be left before a positive number (or empty string) produced by a signed conversion.在有符號數前顯示一個空格 |
‘+’ | A sign character (’+’ or ‘-’) will precede the conversion (overrides a “space” flag). |
conversion type
Conversion | Meaning |
---|---|
‘d’ | Signed integer decimal.有符號十進制整數 |
‘i’ | Signed integer decimal.有符號十進制整數 |
‘o’ | Signed octal value. 有符號八進制數,在數值前會插入八進制前導符’0o’,表示該數是8進制數 |
‘u’ | Obsolete type – it is identical to ‘d’. 廢棄類型,等價於‘d’ |
‘x’ | Signed hexadecimal (lowercase). 小寫16進制有符號數,有前導符’0x’ |
‘X’ | Signed hexadecimal (uppercase). 大寫16進制有符號數,有前導符’0X’ |
‘e’ | Floating point exponential format (lowercase). 指數形式的浮點數(小寫e),默認精度是6位 |
‘E’ | Floating point exponential format (uppercase). 指數形式的浮點數(大寫E),默認精度是6位 |
‘f’ | Floating point decimal format. 小數形式的浮點數,默認精度是6位 |
‘F’ | Floating point decimal format. 小數形式的浮點數,默認精度是6位 |
‘g’ | Floating point format. Uses lowercase exponential format if exponent is less than -4 or not less than precision, decimal format otherwise. 如果 指數 小於-4或 大於等於精度,就用指數形式顯示;否則,用小數形式顯示 |
‘G’ | Floating point format. Uses uppercase exponential format if exponent is less than -4 or not less than precision, decimal format otherwise. |
‘c’ | Single character (accepts integer or single character string). 將values 轉爲單字符 |
‘r’ | String (converts any Python object using repr()). 使用repr()將values 轉爲字符串 |
‘s’ | String (converts any Python object using str()). 使用str()將values 轉爲字符串 |
‘a’ | String (converts any Python object using ascii()). 使用ascii()將values 轉爲字符串 |
‘%’ | No argument is converted, results in a ‘%’ character in the result. ’%%‘將輸出一個% |
代碼實例
# 演示基本用法 % conversion type
conversion_flag1 = '%%d: %d'
conversion_flag2 = '%%o: %#o'
conversion_flag3 = '%%x: %#x, %%X: %#X'
conversion_flag4 = '%%f: %f'
conversion_flag5 = '%%e: %e, %%E: %E'
conversion_flag6 = '%%g: %g'
print(conversion_flag1%16)
print(conversion_flag2%16)
print(conversion_flag3%(16, 16))
print(conversion_flag4%16)
print(conversion_flag5%(16, 16))
print(conversion_flag6%16)
print(conversion_flag6%0.00000123) # 指數小於-4, 以指數形式顯示數字
print(conversion_flag6%12345.1234567)
print(conversion_flag6%10.123)
print(conversion_flag6%123456.123) # 指數等於6, 還是以十進制形式顯示數字
print(conversion_flag6%1234567.123) # 指數大於6, 以指數形式顯示數字
# % mapping_key conversion_type
mapping_key_dict = {'name':'frank', 'age':18}
# mapping_key_dict = {'name':'frank', 'age':'18'}
print("%(name)s"%mapping_key_dict) # 當values 是字典(或mapping type)時, format中的conversion specifier必須是 (字典的key)
# 也就是說,format中的conversion specifier 必須要指定字典的鍵
print("%(name)s,%(age)s"%mapping_key_dict)
# print("%(age)s,%(name)s"%mapping_key_dict)
# 演示 5種conversion flag
# '#' conversion flag, 一般和 %o, %x, %X 結合使用,可以標識 進制,方便閱讀
conversion_flag1 = '%%#d: %#d; %%d: %d'
conversion_flag2 = '%%#o: %#o; %%o: %o'
conversion_flag3 = '%%#x: %#x; %%x: %x'
conversion_flag4 = '%%#f: %#f; %%f: %f'
conversion_flag5 = '%%#e: %#e; %%e: %e'
conversion_flag6 = '%%#g: %#g; %%g: %g'
print(conversion_flag1%(16,16))
print(conversion_flag2%(16,16))
print(conversion_flag3%(16,16))
print(conversion_flag4%(16,16))
print(conversion_flag5%(16,16))
print(conversion_flag6%(16,16))
#'0' conversion flag ,一般和field width結合使用, 當values的實際寬度不夠時,用0來填充
conversion_flag0 = '%010d'
conversion_flag1 = '%010o'
conversion_flag2 = '%010x'
conversion_flag3 = '%010f'
conversion_flag4 = '%010e'
print(conversion_flag0%16)
print(conversion_flag1%16)
print(conversion_flag2%16)
print(conversion_flag3%16)
print(conversion_flag4%16)
#'-' conversion flag, 左對齊; 如果 conversion flag '0'也存在,那麼'0'將無效
conversion_flag0 = '%10d' # 數字的顯示,默認是右對齊
conversion_flag1 = '%-10o'
conversion_flag2 = '%#10x'
conversion_flag3 = '%-10f'
conversion_flag4 = '%-10e'
conversion_flag5 = '%s' # 字符串的顯示,默認是左對齊
print(conversion_flag0%16)
print(conversion_flag1%16)
print(conversion_flag2%16)
print(conversion_flag3%16)
print(conversion_flag4%16)
print(conversion_flag5%"hello")
#'' conversion flag, 空格
conversion_flag0 = '% 010d'
conversion_flag1 = '% 010o'
conversion_flag2 = '% 010x'
conversion_flag3 = '% 010f'
conversion_flag4 = '% 010e'
print(conversion_flag0%16)
print(conversion_flag1%16)
print(conversion_flag2%16)
print(conversion_flag3%16)
print(conversion_flag4%16)
#'+' conversion flag, 正負號標誌
conversion_flag0 = '+:%+010d, -:%+010d'
conversion_flag1 = '+:%+010o, -:%+010o'
conversion_flag2 = '+:%+010x, -:%+010x'
conversion_flag3 = '+:%+010f, -:%+010f'
conversion_flag4 = '+:%+010e, -:%+010e'
print(conversion_flag0%(16, -16))
print(conversion_flag1%(16, -16))
print(conversion_flag2%(16, -16))
print(conversion_flag3%(16, -16))
print(conversion_flag4%(16, -16))
# minimum field width
conversion_flag0 = '%010d'
conversion_flag1 = '%0*d' # 即在values中指定 域寬
print(conversion_flag0%16)
print(conversion_flag1%(5,16))
# precision
conversion_flag0 = '%.2f'
conversion_flag1 = '%.*f' # 因爲精度使用*號表示,所以在values中指定具體精度
print(conversion_flag0%(16))
print(conversion_flag1%(3,16)) # 這裏就是使用3指定精度