python字符串格式化

2018-07-24 08:03:02来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

字符串格式化

 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 \033[43;1m%(name)+60s\033[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')
View Code

 格式化函数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)
View Code

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:python之集合

下一篇:Django项目将debug模式设置为false时,静态文件出错