格式化操作

格式化操作符%

Python风格的字符串格式化操作法只适用于字符串类型,类是于C语言的printf()函数字符串格式化,下面是字符串格式化的符号。

  • 字符串格式化符号

格式化字符转换方式
%c 转换成字符(ASCII码值,或者长度为一的字符串)
%r 优先使用repr()函数进行字符串转换
%s 优先使用str()函数进行字符串转换
%a 优先使用 ascii()函数进行字符串转换
%d/%i 转成有符号十进制数
%u 转成无符号十进制数
%o 转成无符号八进制数
%x%X 转换成无符号十六进制数(x/X代表转换后的十六进制字符的大小写)
%e%E 转换成科学计数法(e/E控制输出e/E)
%f%F 转换成浮点型(小数部分自然截断)
%g%G %e和%f%E和%F的简写
%%  
  • 格式化操作符辅组指令

符号作用
* 定义宽度或者小数点精度
- 用左对齐
+ 在正数前面显示加号(+)
<sp> 在正数前面显示空格
# 在八进制数前面显示'0',在十六进制前面显示'0x' 或者'0X'
0 显示的数字前面填充'0'而不是默认的空格
% '%%'输出一个单一的'%'
(var) 映射变量(字典参数)
m.n m是显示最小总宽度,n是小数点后的位数(好像不能使用)

Python支持两种格式的输入参数。第一种是元组,这基本上是一种的printf()风格的转换参数集。Python支持的第二种形式是字典形式,这种形式里面键是做为格式字符串的出现,相对应的值做为参数在进行转换时提供给格式字符串。总之第二种使用字典形式是出现在反复出现多次,而第二种是需要格式化的内容很多的时候。

使用举例

  • 元组格式举例:

In [2]: 'Defence of the Ancients is %s' %('DOTA',)
Out[2]: 'Defence of the Ancients is DOTA'

In [3]: 'Defence of the Ancients is %s' %'DOTA'
Out[3]: 'Defence of the Ancients is DOTA'

In [4]: 'Defence of the Ancients is %s' %('DOTA')
Out[4]: 'Defence of the Ancients is DOTA'

上面三中方式使用中建议使用第一个,这种方式能够很容易看出格式化的的输入参数是个元组,而下面两种其实也是元组只是由解释器进行操作转换的。

  • 字典格式举例:

In [8]: 'Defence of the Ancients is %s.I like %s' %('DOTA','DOTA')
Out[8]: 'Defence of the Ancients is DOTA.I like DOTA'

In [9]: 'Defence of the Ancients is %(name)s.I like %(name)s' %{'name':'DOTA'}
Out[9]: 'Defence of the Ancients is DOTA.I like DOTA'
  • 十六进制输出:

In [11]: '%x' %108
Out[11]: '6c'

In [12]: '%X' %108
Out[12]: '6C'

In [13]: '%#x' %108
Out[13]: '0x6c'

In [14]: '%#X' %108
Out[14]: '0X6C'
  • 整型、浮点和科学计数法形式输出

In [26]: '%f' %1234.123456789
Out[26]: '1234.123457'

In [28]: '%0.9f' %1234.123456789
Out[28]: '1234.123456789'

In [30]: '%e' %1234.123456789
Out[30]: '1.234123e+03'

In [31]: '%E' %1234.123456789
Out[31]: '1.234123E+03'

In [36]: '%d' %1234.123456789
Out[36]: '1234'

In [37]: '%10d' %1234.123456789
Out[37]: '      1234'

In [38]: '%10.10d' %1234.123456789
Out[38]: '0000001234'

In [39]: '%-10d' %1234.123456789
Out[39]: '1234      '

In [40]: '%+10d' %1234.123456789
Out[40]: '     +1234'

format方法

上面对于字符串格式化输出使用的是格式化操作符,而Python又有一种使用更灵活的格式化方式,就是format方法。

In [45]: 'Defence of the Ancients is {}.'.format('DOTA')
Out[45]: 'Defence of the Ancients is DOTA.'

In [46]: 'Defence of the Ancients is {}.I like {}.2016 begin {}'.format('DOTA','DOTA','TI6')
Out[46]: 'Defence of the Ancients is DOTA.I like DOTA.2016 begin TI6'

In [47]: 'Defence of the Ancients is {1}.I like {2}.2016 begin {0}'.format('TI6','DOTA','DOTA','TI6')
Out[47]: 'Defence of the Ancients is DOTA.I like DOTA.2016 begin TI6'

In [48]: 'Defence of the Ancients is {1}.I like {2}.2016 begin {0}'.format('DOTA','DOTA','TI6')
Out[48]: 'Defence of the Ancients is DOTA.I like TI6.2016 begin DOTA'

In [49]: 'Defence of the Ancients is {0}.I like {0}.2016 begin {1}'.format('DOTA','TI6')
Out[49]: 'Defence of the Ancients is DOTA.I like DOTA.2016 begin TI6'

In [50]: 'Defence of the Ancients is {name}.I like {name}.2016 begin {time}'.format(name='DOTA',time='TI6')
Out[50]: 'Defence of the Ancients is DOTA.I like DOTA.2016 begin TI6'

在使用数字下标定义的时候注意数字不能多于后面字符串的个数,一旦多于会报IndexError的错误。而后面字符串的个数是可以多于前面数字下标的个数的。Python计数是从0开始的,这个需要注意。

  • 混合使用

In [55]: '{} {} {}'.format(1,2,3)
Out[55]: '1 2 3'

In [56]: '{} {} {name}'.format(1,2,name='DOTA')
Out[56]: '1 2 DOTA'

In [70]: '{} {name} {}'.format(1,2,3,name='DOTA')
Out[70]: '1 DOTA 2'

In [63]: '{1} {0} {name}'.format(1,2,name='DOTA')
Out[63]: '2 1 DOTA'

In [67]: '{name} {0} {2}'.format(1,2,3,name='DOTA')
Out[67]: 'DOTA 1 3'
  • 扩展学习类的操作

In [72]: class A:
   ....:     def __init__(self):
   ....:         self.x=1
   ....:         self.y=2
   ....:         

In [73]: a = A()

In [74]: a.x
Out[74]: 1

In [75]: a.y
Out[75]: 2

In [76]: '{0.x},{0.y}'.format(a)
Out[76]: '1,2'

In [87]: '{inst.x}'.format(inst=a)
Out[87]: '1'

In [88]: '{inst.y}'.format(inst=a)
Out[88]: '2'
  • 扩展学习二列表的操作

In [89]: lst
Out[89]: [1, 2, 3, 4]

In [90]: '{0[0]}'.format(lst)
Out[90]: '1'

In [91]: '{0[3]}'.format(lst)
Out[91]: '4'

In [92]: '{lst1[1]}'.format(lst1=lst)
Out[92]: '2'

In [93]: '{lst1[2]}'.format(lst1=lst)
Out[93]: '3'
原文地址:https://www.cnblogs.com/cuchadanfan/p/5926068.html