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.