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指定精度