字符串格式化 format字符串格式化

#字符串格式化
msg = 'I am %s ,my hobby is running.' %'Eileen'
print(msg)

msg1 ='I am %s,my hobby is %s.' %('Eileen','running')
print(msg1)
msg2 = 'I am %s, i am %d year old' %('Eileen',10)
print(msg2)
num = 'prcent %f' %0.989878
num1 = 'prcent %.2f%%' %0.989878
print(num)
print(num1)
msg5 = '【易通航】,您乘坐的%(date)s%(flight)s的航班马上就要起飞了'%{'date':'2020-05-10','flight':'CA112'}
print(msg5)
print('192','1','1','1',sep=':')

#format字符串格式化
tpl = 'I am {},age {},from {}.'.format('alex',18,'China')
print(tpl)
tpl = 'I am {2},age {1},from {0}.'.format('alex',18,'China')
print(tpl)
tpl = 'I am {name},age {age},from {country}.'.format(name ='alex',age = 18,country = 'China')
print(tpl)
tpl = 'I am {name},age {age},from {country}.'.format(**{'name' :'alex','age' : 18,'country' : 'China'})
print(tpl)
tpl = 'I am {1[0]},age {0[1]},from {0[0]}.'.format([1,2,3],[4,5,6])
print(tpl)
tpl = 'I am {:s},age {:d},from {:s}.'.format('alex',18,'China')
print(tpl)
原文地址:https://www.cnblogs.com/lqcjlu/p/12867620.html