Python之格式化輸出:%與format的用法

本文大部分內容來源於此鏈接的文章
,我對部分內容做了一些改動,方便自己日後複習,若有侵權,請聯繫我,我會將此文刪除,謝謝)

話說在看上面鏈接的文章時,並沒有將所有知識學習到位,略過了部分內容,算是偷懶吧,因爲看的確實煩。這篇文章(筆記)對Python的格式化輸出的講解並不完整,不過以後再遇到此類知識時,先看這篇文章,若未找到需要的內容後,再重新百度就好。

1 %用法

1.1 整數的輸出

%o:oct 八進制
%d:dec 十進制
%x:hex 十六進制

>>> print('%o' % 20)
24
>>> print('%d' % 20)
20
>>> print('%x' % 20)
14

1.2 浮點數輸出

%f:保留小數點後面六位有效數字
%.3f:保留小數點後面三位有效數字

%e:保留小數點後面六位有效數字,使用科學計數法
%.3e:保留小數點後面三位有效數字,使用科學計數法

>>> print('%f' % 1.11) # 默認保留6位小數
1.110000
>>> print('%.1f' % 1.11) # 保留1位
1.1
>>> print('%e' % 100.01) # 默認保留6位小數,用科學計數法
1.000100e+02
>>> print('%.3e' % 100.01) # 保留3位小數,用科學計數法
1.000e+02

1.3 字符串輸出

%s:默認的字符串輸出格式
%10s:右對齊,佔位符10位
%-10s:左對齊,佔位符10位
%.2s:截取2位字符串
%10.2s:10位佔位符,截取2位字符串

>>> print('%s' % 'Hello World')
Hello World
>>> print('%20s' % 'Hello World') # 右對齊,取20位,不夠的話以空格補位
         Hello World
>>> print('%-20s' % 'Hello World') # 左對齊,取20位,不夠的話以空格補位
Hello World
>>> print('%.2s' % 'Hello World') # 從左到右截取兩位字符串
He
>>> print('%10.2s' % 'Hello World') # 右對齊,佔位符10位,從左到右截取2位字符串
        He
>>> print('%-10.2s' % 'Hello World') # 左對齊,佔位符10位,從左到右截取2位字符串
He

1.4 其他

字符串格式代碼

在這裏插入圖片描述
常用轉義字符

在這裏插入圖片描述


2 format

相比於%,format()功能更強大。format()函數將字符串當成一個模板,通過傳入的參數進行格式化,使用{}作爲特殊字符來代替%。

2.1 位置匹配

  1. 不帶編號,即"{}"
  2. 帶數字編號,可調換順序,如,"{1}"、"{2}"
  3. 帶關鍵字,如"{name}"、"{age}"
>>> print('{} {}'.format('Hello', 'world'))
Hello world
>>> print('{0} {1}'.format('Hello', 'world')) # 帶數字編號
Hello world
>>> print('{0} {1} -- {1} {0}'.format('Hello', 'world')) # 打亂
Hello world -- world Hello
>>> print('{name} {age} {sex}'.format(name='xw', age='23', sex='male'))  # 使用關鍵字
xw 23 male

2.2 格式轉換

‘b’ - 二進制。將數字以2爲基數進行輸出。
‘c’ - 字符。在打印之前將整數轉換成對應的Unicode字符串。
‘d’ - 十進制整數。將數字以10爲基數進行輸出。
‘o’ - 八進制。將數字以8爲基數進行輸出。
‘x’ - 十六進制。將數字以16爲基數進行輸出,9以上的位數用小寫字母。
‘e’ - 冪符號。用科學計數法打印數字。用’e’表示冪。

>>> print('{:b}'.format(5))
101
>>> print('{:c}'.format(65))
A
>>> print('{:d}'.format(20))
20
>>> print('{:o}'.format(20))
24
>>> print('{:x}'.format(20))
14
>>> print('{:e}'.format(20))
2.000000e+01

2.3 進階

進制轉換

>>> "int:{0:d}, hex:{0:x}, oct:{0:o}, bin:{0:b}".format(42)
'int:42, hex:2a, oct:52, bin:101010'
>>> "int:{0:d}, hex:{0:#x}, oct:{0:#o}, bin:{0:#b}".format(42) # 若進制前有#號,則輸出帶進制前綴
'int:42, hex:0x2a, oct:0o52, bin:0b101010'

左中右對齊及位數補全

<是左對齊(默認狀態就是左對齊),>是右對齊,^是居中。

>>> print('{} and {}'.format('hello', 'world')) # 默認左對齊
hello and world
>>> print('{:10s} and {:>10s}'.format('hello', 'world')) # 第一個取10位左對齊,第二個取十位右對齊
hello      and      world
>>> print('{:^10s} and {:^10s}'.format('hello', 'world')) # 兩個位置均取長10位且居中對齊
  hello    and   world
>>> print('{} is {:.2f}'.format(1.123, 1.123)) # 第二個位置取2位小數
1.123 is 1.12
>>> print('{0} is {0:>10.2f}'.format(1.123)) # 第二個位置取10位,2位爲小數,右對齊
1.123 is       1.12
>>> 
>>> 
>>> '{:<30s}'.format('left aligned') # 左對齊,30位長度
'left aligned                  '
>>> '{:>30s}'.format('right aligned') # 右對齊,30位長度
'                 right aligned'
>>> '{:^30s}'.format('centerd') # 居中
'           centerd            '
>>> '{:*^30}'.format('centerd') # 居中,空白處用*填充
'***********centerd************'

正負符號顯示

>>> '{:+f}  {:+f}'.format(3.14, -3.14) # 總是顯示正負號
'+3.140000  -3.140000'
>>> '{: f}  {: f}'.format(3.14, -3.14) # 若是正數,則在前面留一個空格;若爲負數,則顯示負號即可
' 3.140000  -3.140000'
>>> '{:-f}  {:-f}'.format(3.14, -3.14) # 若爲負數,則顯示負號;若爲正數,則不顯示正號。簡單說來,與'{:f}  {:f}'一致
'3.140000  -3.140000'

百分數

>>> '{:.2%}'.format(0.981) # 以百分數表示,數字部分的小數點後保留2位
'98.10%'

2.4 format的用法變形

直接上代碼

>>> '{0} {1}'.format('hello', 'world') # 普通用法
'hello world'

>>> a = 'hello'
>>> b = 'world'
>>> f'{a} {b}' # f'...',可在字符串前加‘f’符號,以達到格式化的目的,在{}里加入對象,這種爲format的另一種形式
'hello world'

>>> name = 'xw'
>>> age = 23
>>> sex = 'man'
>>> salary = 1234
>>> print(f'My name is {name.capitalize()}.') # 將名字大寫
My name is Xw.
>>> print(f'I am {age:*^10} years old.') # 居中,共佔10個字符,空白處用*填充
I am ****23**** years old.
>>> print(f'I am a {sex}')
I am a man
>>> print(f'My salary is {salary:10.3f}') # 共佔10個字符,小數點後面保留三位
My salary is   1234.000
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章