公众号

Python - 格式化(format())输出

Python函数format() - 格式化输出,例如

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
age = 25
name = 'Caroline'
print('{0} is {1} years old. '.format(name, age)) \#输出参数
print('{0} is a girl. '.format(name))
print('{0:.3} is a decimal. '.format(1/3)) \#小数点后三位
print('{0:\_^11} is a 11 length. '.format(name)) \#使用_补齐空位
print('{first} is as {second}. '.format(first=name, second='Wendy')) \#别名替换
print('My name is {0.name}'.format(open('out.txt', 'w'))) \#调用方法
print('My name is {0:8}.'.format('Fred')) \#指定宽度
```
输出:

Caroline is 25 years old.
Caroline is a girl.
0.333 is a decimal.
_Caroline__ is a 11 length.
Caroline is as Wendy.
My name is out.txt
My name is Fred.
```

最后想说:Fork me on GitHub 欢迎 Star

🙈坚持原创技术分享,您的支持将鼓励我继续创作🙈