python 字符串格式化调用方法

'''
字符串对象的format方法使用主体字符串作为模板,并且接受任意多个表示将要根据模板替换的值的参数
在主体字符串中,花括号通过位置(例如{1})或关键字(例如{food})指出替换目标及将要插入的参数
'''
template='{0},{1} and {2}'
print(template.format('spam','ham','eggs'))    #spam,ham and eggs
template='{motto},{pork} and {food}'
print(template.format(motto='spam',pork='ham',food='eggs'))    #spam,ham and eggs
template='{motto},{0} and {food}'
print(template.format('ham',motto='spam',food='eggs'))    #spam,ham and eggs
x='{motto},{0} and {food}'.format(42, motto=3.14, food=[1,2])
print(x)    #3.14,42 and [1, 2]
print(x.split('and'))   #['3.14,42 ', ' [1, 2]']
print(x.replace('and', 'but under no circumstances'))   #3.14,42 but under no circumstances [1, 2]

#格式化字符串可以指定对象属性和字典键,方括号指定字典键,而点表示位置或关键字所引用的一项的对象属性
import sys
print('my {1[spam]} runs {0.platform}'.format(sys, {'spam':'laptop'}))   #0表示sys,0.platform是sys的属性,1表示字典:{'spam':'laptop'}[spam]     my laptop runs win32
print('my {config[spam]} runs {sys.platform}'.format(sys=sys, config={'spam':'laptop'}))    #my laptop runs win32

#格式化字符串可以指定列表偏移量已执行索引,但是,只有单个的正的偏移才能在格式化字符串的语法中有效
somelist=list('SPAM')
print(somelist)
print('first={0[0]}, third={0[2]}'.format(somelist))    #first=S, third=A
print('first={0}, last={1}'.format(somelist[0], somelist[-1]))     #first=S, last=M
parts=somelist[0], somelist[-1], somelist[1:3]
print('first={0}, last={1}, middle={2}'.format(*parts))     #first=S, last=M, middle=['P', 'A']

'''
添加具体格式化
在替换目标的标识之后使用给一个冒号,后面跟着可以指定字段大小,对齐方式和一个特定类型编码的格式化声明
结构:{fieldname!conversionflag:formatspec}
fieldname是指定参数的一个数字或关键字,后面跟着可选的".name"或"[index]"成分引用
conversionflag可以是r、s,或者a分别是在该值上对repr,str或ascii内置函数的一次调用
formatspec指定了如何表示该值,包括字段宽度、对齐方式、补零、小数点精度等细节,并且以一个可选的数据类型编码结束
formatspec形式:[[fill]align][sign][#][0][width][.precision][typecode]
align可能是<、>、=、^、分别表示左对齐、右对齐、一个标记字符后的补充或居中对齐,formatspec也包含嵌套的,只带有{}的格式化字符串,它从参数列表动态的获取值(和格式化表达式中的*很相似)
格式化方法还允许一个"b"类型编码用来以二进制格式显示整数(等同于使用bin内置函数)
允许一个"%"类型编码来显示百分比
'''
import sys
print('...{0:10}...{1:10}...'.format('spam', 123.4567))    #10表示宽度10   ...spam      ...  123.4567...
print('...{0:<10}...{1:<10}...'.format('spam', 123.4567))   #<10表示宽度10,左对齐   ...spam      ...123.4567  ...
print('...{0:>10}...{1:>10}...'.format('spam', 123.4567))   #>10表示宽度10,右对齐   ...      spam...  123.4567...
print('...{0:^10}...{1:^10}...'.format('spam', 123.4567))   #>10表示宽度10,居中对齐   ...   spam   ... 123.4567 ...
print('...{0.platform:>10}={1[item]:<10}...'.format(sys, dict(item='laptop')))   #...     win32=laptop    ...

print('{0:e},{1:.3e},{1:g}'.format(3.14159,3.14159,3.14159))   #冒号后表示小数位数和数据类型3.141590e+00,3.142e+00,3.14159
print('{0:f},{1:.2f},{2:06.2f}'.format(3.14159,3.14159,3.14159))    #3.141590,3.14,003.14

print('{0:x},{1:o},{2:b}'.format(255,255,255))   #十六进制,八进制,二进制ff,377,11111111

print('{0:.2f}'.format(1/3.0))   #0.33
print('%.2f'%(1/3.0))   #0.33
print('{0:.{1}f}'.format(1/3.0,4))   #0.3333
print('%.*f' %(4,1/3.0))   #0.3333

#字符串格式化方法的一种更简洁的替代方法
print('{0:.2f}'.format(1.2345))     #1.23
print(format(1.2345, '.2f'))     #1.23
print('%.2f'% 1.2345)     #1.23

#千分位分割
 
 
print('{0:d}'.format(9999999999))   #9999999999
print('{0:,d}'.format(9999999999))   #9,999,999,999
print('{0:,.2f}'.format(9999999.999))   #10,000,000.00

#二进制格式
print('{0:b}'.format(255))    #11111111

#数字替换值输入可选
print('the {} side {} {}'.format('a','b','c'))

猜你喜欢

转载自blog.csdn.net/weixin_37016700/article/details/78679987