字符串格式化
1 # msg='i am %s my hobby is %s' % ('lhf','alex') 2 # print(msg) 3 # 4 # msg='i am %s my hobby is %s' % ('lhf',1) 5 # msg='i am %s my hobby is %s' % ('lhf',[1,2]) 6 # print(msg) 7 # name='lhf' 8 # age=19 9 # msg='i am %s my hobby is %s' % (name,age) 10 # print(msg) 11 12 #打印浮点数 13 # tpl = "percent %.2f" % 99.976234444444444444 14 # print(tpl) 15 16 #打印百分比 17 # tpl = 'percent %.2f %%' % 99.976234444444444444 18 # print(tpl) 19 20 # tpl = "i am %(name)s age %(age)d" % {"name": "alex", "age": 18} 21 # print(tpl) 22 23 # msg='i am %(name)+5s my hobby is alex' %{'name':'lhf'} 24 # print(msg) 25 # 26 # msg='i am 33[43;1m%(name)+60s 33[0m my hobby is alex' %{'name':'lhf'} 27 # print(msg) 28 29 30 # print('root','x','0','0',sep=':') 31 # print('root'+':'+'x'+':'+'0','0')
格式化函数format()
1 # tpl = "i am {name}, age {age}, really {name}".format(name="seven", age=18) 2 # print(tpl) 3 # 4 # tpl = "i am {name}, age {age}, really {name}".format(**{"name": "seven", "age": 18}) 5 # print(tpl) 6 # tpl = "i am {:s}, age {:d}".format(*["seven", 18]) 7 # print(tpl) 8 # tpl = "i am {:s}, age {:d}".format("seven", 18) #["seven", 18] 9 # print(tpl) 10 # 11 # l=["seven", 18] 12 # tpl = "i am {:s}, age {:d}".format('seven',18) 13 # print(tpl) 14 15 # tpl = "numbers: {:b},{:o},{:d},{:x},{:X}, {:%},{}".format(15, 15, 15, 15, 15, 15.87623, 2) 16 # print(tpl)