常用的占位符

1,
d digits
s string
f floating

占位符替换内容
%d整数
%s字符串
%f浮点数

注意:d前面的数字代表要留的空格,看执行出来的结果即可发现规律。

print('%d' %3)
print('%2d' %3)
print('%3d' %3) 
>>> format = "Hello, %s. %s enough for ya?" 
>>> values = ('world', 'Hot')
>>> format % values
'Hello, world. Hot enough for ya?'
➜  pythoncode python3 holly_02_29.py
3
 3
  3
for i in range(20):
	print('%2d-%02d' % (i,i*2))
➜  pythoncode python3 holly_02_29.py
 0-00
 1-02
 2-04
 3-06
 4-08
 5-10
 6-12
 7-14
 8-16
 9-18
10-20
11-22
12-24
13-26
14-28
15-30
16-32
17-34
18-36
19-38
print('age:%s.gender:%s' % (25,'true'))
print('%2d-%02d' % (3, 1))
print('hello,%s,you are %s' % ('holly','good'))

print('%2d-%2d' % (3, 1))
print('%.2f' % 3.1415926)
print ('Age: %s. Gender: %s' % (25, True))
print('hello,%s'%'world')
print('hi,%s,you have $%d.' % ('Michael',1000)) 


```python
➜  pythoncode python3 holly_02_29.py
age:25.gender:true
 3-01
hello,holly,you are good
 3- 1
3.14
Age: 25. Gender: True
hello,world
hi,Michael,you have $1000.
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值