python基础篇学习(八)之格式化输出
一、%
1.基本用法
info = 'my name is %s, I am from %s.' % ('小圆子', '江苏')
print(info)
info = 'my name is %s, I am from %s.' % ('江苏', '小圆子')
print(info)
info = 'my name is %s' % '小圆子'
print(info)
info = 'my name is %(name)s, I am from %(hometown)s.' % {'hometown': '江苏', 'name': '小圆子'}
print(info)
info = 'my name is %(name)s%(name)s, I am from %(hometown)s%(hometown)s.' % {'hometown': '江苏', 'name': '小圆子'}
print(info)
info = 'my name is %s' % 16
print(info)
info = 'my name is %s' % ['a', 'b']
print(info)
info = 'my name is %s' % {'a': 'aa', 'b': 'bb'}
print(info)
----------------------------------------------------------------
# 运行结果:
my name is 小圆子, I am from 江苏.
my name is 江苏, I am from 小圆子.
my name is 小圆子
my name is 小圆子, I am from 江苏.
my name is 小圆子小圆子, I am from 江苏江苏.
my name is 16
my name is ['a', 'b']
my name is {'a': 'aa', 'b': 'bb'}
2.%s可以接收任意类型的值,%d只能接收整型
info = 'my name is %d' % 16
print(info)
info = 'my name is %d' % 'a'
print(info)
----------------------------------------------------------------
# 运行结果:
Traceback (most recent call last):
File "E:\Python-workspace\python-workspace\teach\05-格式化输出.py", line 35, in <module>
info = 'my name is %d' % 'a'
TypeError: %d format: a number is required, not str
my name is 16
3.小数精度控制
info = 'my name is %.2f' % 3.1415926
print(info)
----------------------------------------------------------------
# 运行结果:
my name is 3.14
二、format()
1.基本用法
info = 'my name is {}, I am from {}.'.format('小圆子', '江苏')
print(info)
info = 'my name is {0}, I am from {1}.'.format('小圆子', '江苏')
print(info)
info = 'my name is {1}, I am from {0}.'.format('小圆子', '江苏')
print(info)
info = 'my name is {0}{0}{0}, I am from {1}{1}{1}.'.format('小圆子', '江苏')
print(info)
info = 'my name is {name}, I am from {hometown}.'.format(name='小圆子', hometown='江苏')
print(info)
----------------------------------------------------------------
# 运行结果:
my name is 小圆子, I am from 江苏.
my name is 小圆子, I am from 江苏.
my name is 江苏, I am from 小圆子.
my name is 小圆子小圆子小圆子, I am from 江苏江苏江苏.
my name is 小圆子, I am from 江苏.
2.格式化填充
a = '{0:*^10}'.format('开始')
print(a)
a = '{0:*<10}'.format('开始')
print(a)
a = '{0:*>10}'.format('开始')
print(a)
a = '{:*^10}'.format('开始')
print(a)
a = '{aa:*^10}'.format(aa='开始')
print(a)
a = '{aa:-^10}'.format(aa='开始')
print(a)
a = '{aa:6^10}'.format(aa='开始')
print(a)
a = '{aa:圆^10}'.format(aa='开始')
print(a)
----------------------------------------------------------------
# 运行结果:
****开始****
开始********
********开始
****开始****
****开始****
----开始----
6666开始6666
圆圆圆圆开始圆圆圆圆
3.小数精度控制
b = '{num:.2f}'.format(num=3.1415926)
print(b)
b = '{0:.2f}'.format(3.1415926)
print(b)
b = '{:.2f}'.format(3.1415926)
print(b)
----------------------------------------------------------------
# 运行结果:
3.14
3.14
3.14
三、f
1.基本用法
name = input('请输入你的名字:')
hometown = input('你来自哪里:')
info = f'我的名字是{name},我来自{hometown}'
print(info)
----------------------------------------------------------------
# 运行结果:
请输入你的名字:小圆子
你来自哪里:china
我的名字是小圆子,我来自china
2.小数精度控制
num = 3.1415926
print(f'{num:.2f}')
----------------------------------------------------------------
# 运行结果:
3.14
总结:
速度上—f这种方式是速度最快的,其次是format(),最慢的是%的方式,因此用的最多的就是f和format()这两种方式
兼容性问题—如果写的代码可能要拿到python2.7或者python3.5以下的python环境运行,那就优先使用format这种方式,如果只是运行在python3.5以上的,那就优先使用f