一:%用法
1、整數輸出
%o —— oct 八進制
%d —— dec 十進制
%x —— hex 十六進制
print('%o' % 20) # 24 print('%d' % 20) # 20 print('%x' % 20) # 14
2、浮點數輸出
%f ——保留小數點后面六位有效數字
%.3f,保留3位小數位
%e ——保留小數點后面六位有效數字,指數形式輸出
%.3e,保留3位小數位,使用科學計數法
%g ——在保證六位有效數字的前提下,使用小數方式,否則使用科學計數法
%.3g,保留3位有效數字,使用小數或科學計數法
print('%f' % 1.11) # 默認保留6位小數 1.110000 print('%.1f' % 1.11) # 取1位小數 1.1 print('%e' % 1.11) # 默認6位小數,用科學計數法 1.110000e+00 print('%.3e' % 1.11) # 取3位小數,用科學計數法 1.110e+00 print('%g' % 1111.1111) # 默認6位有效數字 1111.11 print('%.7g' % 1111.1111) # 取7位有效數字 1111.111 print('%.2g' % 1111.1111) # 取2位有效數字,自動轉換為科學計數法 1.1e+03
3、字符串輸出
%s
%10s——右對齊,占位符10位
%-10s——左對齊,占位符10位
%.2s——截取2位字符串
%10.2s——10位占位符,截取兩位字符串
print('%s' % 'hello world') # 字符串輸出 hello world print('%20s' % 'hello world') # 右對齊,取20位,不夠則補位 hello world print('%-20s' % 'hello world') # 左對齊,取20位,不夠則補位 hello world print('%.2s' % 'hello world') # 取2位 he print('%10.2s' % 'hello world') # 右對齊,取2位 he print('%-10.2s' % 'hello world') # 左對齊,取2位 he
4、其他

二:format用法
相對基本格式化輸出采用‘%’的方法,format()功能更強大,該函數把字符串當成一個模板,通過傳入的參數進行格式化,並且使用大括號‘{}’作為特殊字符代替‘%’
1、位置匹配
(1)不帶編號,即“{}”
(2)帶數字編號,可調換順序,即“{1}”、“{2}”
(3)帶關鍵字,即“{a}”、“{tom}”
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('{1} {1} {0}'.format('hello','world')) # world world hello print('{a} {tom} {a}'.format(tom='hello',a='world')) # 帶關鍵字 world hello world # 通過位置匹配 print('{0}, {1}, {2}'.format('a', 'b', 'c')) # a, b, c print('{}, {}, {}'.format('a', 'b', 'c')) # 3.1+版本支持 a, b, c print('{2}, {1}, {0}'.format('a', 'b', 'c')) # c, b, a print('{2}, {1}, {0}'.format(*'abc')) # 可打亂順序 c, b, a print('{0}{1}{0}'.format('abra', 'cad')) # 可重復 abracadabra # 通過類的屬性匹配 class Point: def __init__(self, x, y): self.x, self.y = x, y def __str__(self): return 'Point({self.x}, {self.y})'.format(self=self) print(str(Point(4, 2))) # Point(4, 2) # 通過下標或者key匹配參數 coord = (3, 5) print('X: {0[0]}; Y: {0[1]}'.format(coord)) # X: 3; Y: 5 a = {'a': 'test_a', 'b': 'test_b'} print('X: {0[a]}; Y: {0[b]}'.format(a)) # X: test_a; Y: test_b
2、格式轉換
'b' - 二進制。將數字以2為基數進行輸出。
'c' - 字符。在打印之前將整數轉換成對應的Unicode字符串。
'd' - 十進制整數。將數字以10為基數進行輸出。
'o' - 八進制。將數字以8為基數進行輸出。
'x' - 十六進制。將數字以16為基數進行輸出,9以上的位數用小寫字母。
'e' - 冪符號。用科學計數法打印數字。用'e'表示冪。
'g' - 一般格式。將數值以fixed-point格式輸出。當數值特別大的時候,用冪形式打印。
'n' - 數字。當值為整數時和'd'相同,值為浮點數時和'g'相同。不同的是它會根據區域設置插入數字分隔符。
'%' - 百分數。將數值乘以100然后以fixed-point('f')格式打印,值后面會有一個百分號。
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%
3、format變形用法
# f"xxxx" # 可在字符串前加f以達到格式化的目的,在{}里加入對象,此為format的另一種形式: a = "hello" b = "world" print(f"{a} {b}") name = 'jack' age = 18 sex = 'man' job = "IT" salary = 9999.99 print(f'my name is {name.capitalize()}.') # my name is Jack. print(f'I am {age:*^10} years old.') # I am ****18**** years old. print(f'I am a {sex}') # I am a man print(f'My salary is {salary:10.3f}') # My salary is 9999.990
