print('%s' % 'hello world') # 结果:hello world # 右对齐,取20位,不够则补位 print('%20s' % 'hello world') # 结果: hello world # 左对齐,取20位,不够则补位 print('%-20s' % 'hello world') # 结果:hello world # 取2位 print('%.2s' % 'hello world') # 结果:he # 右对齐,占位符10位,取2位 print('%10.2s' % 'hello world') # 结果: he # 左对齐,占位符10位,取2位 print('%-10.2s' % 'hello world') # 结果:he
浮点数输出(%f)

%f ——保留小数点后面六位有效数字
%.3f,保留3位小数位
%e ——保留小数点后面六位有效数字,指数形式输出
%.3e,保留3位小数位,使用科学计数法
%g ——在保证六位有效数字的前提下,使用小数方式,否则使用科学计数法
%.3g,保留3位有效数字,使用小数或科学计数法

# 默认保留6位小数
print('%f' % 1.11)    # 1.110000
#  取1位小数
print('%.1f' % 1.11)    # 结果:1.1
# 默认6位小数,用科学计数法
print('%e' % 1.11)    # 结果:1.110000e+00
# 取3位小数,用科学计数法
print('%.3e' % 1.11)    # 结果:1.110e+00
# 默认6位有效数字
print('%g' % 1111.1111)    # 结果:1111.11
# 取7位有效数字
print('%.7g' % 1111.1111)    # 结果:1111.111
# 取2位有效数字,自动转换为科学计数法
print('%.2g' % 1111.1111)    # 结果:1.1e+03

format的使用

① 不带参数,即{}
② 带数字参数,可调换顺序,即{1}、{2}
③ 带关键字,即{a}、{to}

# 不带参数
print('{} {}'.format('hello','world'))    # 结果:hello world
# 带数字参数
print('{0} {1}'.format('hello','world'))    # 结果:hello world
# 参数顺序倒乱
print('{0} {1} {0}'.format('hello','world'))    # 结果:hello world hello
# 带关键字参数
print('{a} {tom} {a}'.format(tom='hello',a='world'))    # 结果:world hello world
# 通过索引
coord = (3, 5)
print('X: {0[0]};  Y: {0[1]}'.format(coord))    # 结果:'X: 3;  Y: 5'
# 通过key键参数
a = {'a': 'test_a', 'b': 'test_b'}
print('X: {0[a]};  Y: {0[b]}'.format(a))    # 结果:'X: test_a;  Y: test_b'
print('{0:b}'.format(3))    # 结果:11
print('{:c}'.format(20))    # 结果:�
print('{:d}'.format(20))    # 结果:20
print('{:o}'.format(20))    # 结果:24
print('{:x}'.format(20))    # 结果:14
print('{:e}'.format(20))    # 结果:2.000000e+01
print('{:g}'.format(20.1))    # 结果:20.1
print('{:f}'.format(20))    # 结果:20.000000
print('{:n}'.format(20))    # 结果:20
print('{:%}'.format(20))    # 结果:2000.000000%
print("int: {0:d};  hex: {0:x};  oct: {0:o};  bin: {0:b}".format(42))
# 输出:int: 42;  hex: 2a;  oct: 52;  bin: 101010
print("int: {0:d};  hex: {0:#x};  oct: {0:#o};  bin: {0:#b}".format(42))
# 在前面加“#”,则带进制前缀
# 输出:int: 42;  hex: 0x2a;  oct: 0o52;  bin: 0b101010

左中右对齐及位数补全

㈡ 取位数 “{:4s}”、"{:.2f}"等

# 默认左对齐
print('{} and {}'.format('hello','world'))    # 结果:hello and world
# 取10位左对齐,取10位右对齐
print('{:10s} and {:>10s}'.format('hello','world'))   # 结果:hello      and      world
# 取10位中间对齐
print('{:^10s} and {:^10s}'.format('hello','world'))    # 结果:  hello    and   world   
# 取2位小数
print('{} is {:.2f}'.format(1.123,1.123))    # 结果:1.123 is 1.12
# 取2位小数,右对齐,取10位
print('{0} is {0:>10.2f}'.format(1.123))    # 结果:1.123 is       1.12
# 左对齐
print('{:<30}'.format('left aligned'))     # 结果:'left aligned                  '
# 右对齐
print('{:>30}'.format('right aligned'))    # 结果:'                 right aligned'
# 中间对齐
print('{:^30}'.format('centered'))    # 结果:'           centered           '
# 使用“*”填充
print('{:*^30}'.format('centered'))    # 结果:'***********centered***********'
# 还有“=”只能应用于数字,这种方法可用“>”代替
print('{:0=30}'.format(11))    # '000000000000000000000000000011'

正负符号显示
正负符号显示 %+f, %-f, 和 % f的用法

# 总是显示符号
print('{:+f}; {:+f}'.format(3.14, -3.14))    # '+3.140000; -3.140000'
# 若是+数,则在前面留空格
print('{: f}; {: f}'.format(3.14, -3.14))    # ' 3.140000; -3.140000'
# -数时显示-,与'{:f}; {:f}'一致
print('{:-f}; {:-f}'.format(3.14, -3.14))    # '3.140000; -3.140000'
points = 19
total = 22
print('Correct answers: {:.2%}'.format(points/total))    # 'Correct answers: 86.36%'

逗号作为千位分隔符,金额表示

print('{:,}'.format(1234567890))    # '1,234,567,890'
format变形用法

在字符串前加f以达到格式化的目的,在{}里加入对象,此为format的另一种形式

name = 'jack'
age = 18
sex = 'man'
job = "IT"
salary = 9999.99