天天看點

format()方法的基本使用1,使用格式2,格式控制

1,使用格式

<模闆字元串>.format(參數)

>>> "{},{}".format('python','大法好')
'python,大法好'
           

2,格式控制

“{<參數序号>:<填充><對齊><寬度><,千位分隔符><精度><類型>}”

1. 填充

>>> s='python'
>>> "{:*>30}".format(s)#用*填充
'************************python'
           

2. 對齊

>>> s='python'
>>> "{:*>30}".format(s)#>右對齊<左對齊^居中對齊
'************************python'
           

3. 寬度

s=’python’

“{:*>30}”.format(s)#寬度30

‘************************python’

4. 千位分隔符

>>> "{:,.2f}".format(a)
'12,345.68'
           

5. 精度

. 精度:浮點數保留位數|字元串保留位數

>>> "{:,.2f}".format(a)
'12,345.68'
           

6. 類型

>>> "{:,.2f}".format(a)
'12,345.68'