Python的內置函數format()

#format()的(槽){}是從0開始的
>>> '{0} love {1}'.format('I','you')
'I love you'
>>> '{1} love {0}'.format('I','you')
'you love I'
>>> '{-1} love {0}'.format('I','you')
Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    '{-1} love {0}'.format('I','you')
KeyError: '-1'

#format()的{}(槽)出現次數和format()方法中出數量不一致,則必須在槽中用序號指定參數使用
>>> '{} love {}'.format('I')
Traceback (most recent call last):
  File "<pyshell#3>", line 1, in <module>
    '{} love {}'.format('I')
IndexError: tuple index out of range
>>> '{0} love {0}'.format('I')
'I love I'
#format()的(槽){(引導符):(填充)(對齊)(width)(,(千元分隔符))(.(精度))(類型)}格式控制
>>> a = '我正在學習Python!'
>>> '{:15}'.format(a)                #默認左對齊
'我正在學習Python!   '
>>> a
'我正在學習Python!'
>>> '{:1}'.format(a)                #指定寬讀爲1
'我正在學習Python!'

>>> '{:<30}'.format(a)                    #左對齊30寬度
'我正在學習Python!                  '
>>> '{:>30}'.format(a)                    #右對齊30寬度
'                  我正在學習Python!'
>>> '{:^30}'.format(a)                    #居中對齊30寬度
'         我正在學習Python!         '
>>> '{:=^30}'.format(a)                   #填充=並居中對齊30寬度
'=========我正在學習Python!========='

#format()的{}(槽)一定要按順序是:(填充)(對齊)(寬度)(,(千元分隔符))(.(精度))(類型)
>>> '{:=30}'.format(a)
Traceback (most recent call last):
  File "<pyshell#13>", line 1, in <module>
    '{:=30}'.format(a)
ValueError: '=' alignment not allowed in string format specifier  #'='在字符串格式說明符中不允許對齊


#format()的格式控制標記可以用變量表示,則用槽{}來指定所對應的控制標記及數量
>>> a = '我正在學習Python!'
>>> x = '#'
>>> y = '>'
>>> w = 30
>>> '{0:{1}{2}{3}}'.format(a,x,y,w)
'##################我正在學習Python!'


>>> '{1:{0}{1}{2}}'.format(x,y,w,a)                  #指定顯示填充和對齊
'#############################>'
>>> '{2:{0}{1}{2}}'.format(x,y,w,a)                  #指定顯示填充和寬度
'############################30'
>>> '{3:{0}{1}{2}}'.format(x,y,w,a)                  #要看參數的順序,才指定參數的序號
'##################我正在學習Python!'

>>> '{1:{1}{2}{3}}'.format(a,x,y,w)                  #指定顯示填充
'##############################'
>>> '{2:{1}{2}{3}}'.format(a,x,y,w)                  #指定顯示填充和對齊
'#############################>'
>>> '{3:{1}{2}{3}}'.format(a,x,y,w)                  #指定顯示填充和寬度  
'############################30'
>>> '{0:{1}{2}{3}}'.format(a,x,y,w)                  #指定顯示所有參數
'##################我正在學習Python!'

#不能從自動字段編號切換到手動字段規範
>>> '{:{1}{2}{3}}'.format(a,x,y,w)
Traceback (most recent call last):
  File "<pyshell#29>", line 1, in <module>
    '{:{1}{2}{3}}'.format(a,x,y,w)
ValueError: cannot switch from automatic field numbering to manual field specification
#format()的千位分隔符控制格式
>>> a = 12345678
>>> '{:~^20}'.format(a)
'~~~~~~12345678~~~~~~'
>>> '{:~^20,}'.format(a)
'~~~~~12,345,678~~~~~'


#format()的精度和類型控制格式
>>> '{:.2f}'.format(a)
'123.46'
>>> '{:.1f}'.format(a)
'123.5'

#格式說明符缺少精度
>>> '{:.f}'.format(a)
Traceback (most recent call last):
  File "<pyshell#39>", line 1, in <module>
    '{:.f}'.format(a)
ValueError: Format specifier missing precision


>>> '{:~^20.3f}'.format(a)
'~~~~~~123.457~~~~~~~'

#format()所有的控制格式
>>> '{:~^20,.2f}'.format(a)
'~~~~~~1,234.57~~~~~~'
>>> '{:~^20,.3f}'.format(a)
'~~~~~1,234.568~~~~~~'

#format()對str(字符串)的精度控制格式,即對字符串數量的控制
>>> a = '我在學習Python!'
>>> '{:.10}'.format(a)
'我在學習Python'
>>> '{:.5}'.format(a)
'我在學習P'
#整數類型格式化
>>> '二進制:{0:b},八進制:{0:o},十進制:{0:d},小寫十六進制:{0:x},大寫十六進制:{0:X},Unicode字符:{0:c}'.format(0x1c8)
	  
'二進制:111001000,八進制:710,十進制:456,小寫十六進制:1c8,大寫十六進制:1C8,Unicode字符:Lj'

#浮點數的格式化
>>> '小寫字母e的指數{0:e},大寫字母E的指數{0:E},標準浮點{0:f},百分比{0:%}'.format(1.25)
	  
'小寫字母e的指數1.250000e+00,大寫字母E的指數1.250000E+00,標準浮點1.250000,百分比125.000000%'

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章