字符串格式化

 1 #coding=utf-8
 2 '''
 3 字符串格式化
 4 '''
 5 
 6 #~~~~~~替换元素作用
 7 #默认顺序替换
 8 print('hello,{},{},{}'.format('z','c','zc'))
 9 
10 #使用关键字进行对应变量的替换
11 print('hello,{y},{x},{z}'.format(x='z',y='c',z='zc'))
12 
13 #使用索引序号进行替换
14 print('hello,{1},{0},{2}'.format('z','c','zc'))
15 print('hello,{1[1]},{0},{2}'.format('z',('c','cc','ccc'),'zc'))
16 print('hello,{1[1]},{0[1]},{2}'.format(('z','zz','zzz'),('c','cc','ccc'),'zc'))
17 
18 #使用关键字和索引序号进行对应变量的替换
19 print('hello,{1[1]},{0[1]},{x}'.format(('z','zz','zzz'),('c','cc','ccc'),x='zc'))
20 
21 
22 #~~~~~~规范格式
23 #指定精度
24 print('此次的小数是:{:.5f}'.format(3.1415926))
25 print('此次的小数是:{:.2f}'.format(3.1415926))
26 
27 #指定进制
28 print('13的二进制是:{:b}'.format(13))
29 print('13的八进制是:{:o}'.format(13))
30 print('13的十进制是:{:d}'.format(13))
31 print('13的十六进制是:{:x}'.format(13))
32 
33 #对齐方式
34 #print('x表示填充的样式,>表示右对齐,y表示要展示的宽度{:x>y}'.format(13))
35 print('x表示填充的样式,>表示右对齐,y表示要展示的宽度{:x>10}'.format(13))
36 print('x表示填充的样式,<表示左对齐,y表示要展示的宽度{:x<10}'.format(13))
37 print('x表示填充的样式,^表示居中对齐,y表示要展示的宽度{:x^10}'.format(13))

 

转载于:https://www.cnblogs.com/zc13/p/9466945.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值