字符串格式化

1、百分號(hào)方式

%[(name)][flags][width].[precision]typecode

注:Python中百分號(hào)格式化是不存在自動(dòng)將整數(shù)轉(zhuǎn)換成二進(jìn)制表示的方式

常用格式化:

tpl = "i am %s" % "alex"
 
tpl = "i am %s age %d" % ("alex", 18)
 
tpl = "i am %(name)s age %(age)d" % {"name": "alex", "age": 18}
 
tpl = "percent %.2f" % 99.97623
 
tpl = "i am %(pp).2f" % {"pp": 123.425556, }
 
tpl = "i am %.2f %%" % {"pp": 123.425556, }

2、format方式

[[fill]align][sign][#][0][width][,][.precision][type]

  • fill 【可選】空白處填充的字符
  • align 【可選】對(duì)齊方式(需配合width使用)
    - <,內(nèi)容左對(duì)齊
    - >,內(nèi)容右對(duì)齊(默認(rèn))
    - =,內(nèi)容右對(duì)齊,將符號(hào)放置在填充字符的左側(cè),且只對(duì)數(shù)字類型有效。 即使:符號(hào)+填充物+數(shù)字
    - ^,內(nèi)容居中
  • sign 【可選】有無符號(hào)數(shù)字
    - +,正號(hào)加正,負(fù)號(hào)加負(fù);
    - -,正號(hào)不變,負(fù)號(hào)加負(fù);
    - 空格 ,正號(hào)空格,負(fù)號(hào)加負(fù);
  • 【可選】對(duì)于二進(jìn)制、八進(jìn)制、十六進(jìn)制,如果加上#,會(huì)顯示 0b/0o/0x,否則不顯示

  • , 【可選】為數(shù)字添加分隔符,如:1,000,000
  • width 【可選】格式化位所占寬度
  • .precision 【可選】小數(shù)位保留精度
  • type 【可選】格式化類型
    - 傳入” 字符串類型 “的參數(shù)
    -s,格式化字符串類型數(shù)據(jù)
    - 空白,未指定類型,則默認(rèn)是None,同s
    - 傳入“ 整數(shù)類型 ”的參數(shù)
    - b,將10進(jìn)制整數(shù)自動(dòng)轉(zhuǎn)換成2進(jìn)制表示然后格式化
    - c,將10進(jìn)制整數(shù)自動(dòng)轉(zhuǎn)換為其對(duì)應(yīng)的unicode字符
    - d,十進(jìn)制整數(shù)
    - o,將10進(jìn)制整數(shù)自動(dòng)轉(zhuǎn)換成8進(jìn)制表示然后格式化;
    - x,將10進(jìn)制整數(shù)自動(dòng)轉(zhuǎn)換成16進(jìn)制表示然后格式化(小寫x)
    - X,將10進(jìn)制整數(shù)自動(dòng)轉(zhuǎn)換成16進(jìn)制表示然后格式化(大寫X)
    傳入“ 浮點(diǎn)型或小數(shù)類型 ”的參數(shù)
    - e, 轉(zhuǎn)換為科學(xué)計(jì)數(shù)法(小寫e)表示,然后格式化;
    - E, 轉(zhuǎn)換為科學(xué)計(jì)數(shù)法(大寫E)表示,然后格式化;
    - f , 轉(zhuǎn)換為浮點(diǎn)型(默認(rèn)小數(shù)點(diǎn)后保留6位)表示,然后格式化;
    - F, 轉(zhuǎn)換為浮點(diǎn)型(默認(rèn)小數(shù)點(diǎn)后保留6位)表示,然后格式化;
    - g, 自動(dòng)在e和f中切換
    - G, 自動(dòng)在E和F中切換
    - %,顯示百分比(默認(rèn)顯示小數(shù)點(diǎn)后6位)

常用格式化

tpl = "i am {}, age {}, {}".format("seven", 18, 'alex')
  
tpl = "i am {}, age {}, {}".format(*["seven", 18, 'alex'])
  
tpl = "i am {0}, age {1}, really {0}".format("seven", 18)
  
tpl = "i am {0}, age {1}, really {0}".format(*["seven", 18])
  
tpl = "i am {name}, age {age}, really {name}".format(name="seven", age=18)
  
tpl = "i am {name}, age {age}, really {name}".format(**{"name": "seven", "age": 18})
  
tpl = "i am {0[0]}, age {0[1]}, really {0[2]}".format([1, 2, 3], [11, 22, 33])
  
tpl = "i am {:s}, age {:d}, money {:f}".format("seven", 18, 88888.1)
  
tpl = "i am {:s}, age {:d}".format(*["seven", 18])
  
tpl = "i am {name:s}, age {age:d}".format(name="seven", age=18)
  
tpl = "i am {name:s}, age {age:d}".format(**{"name": "seven", "age": 18})
 
tpl = "numbers: {:b},{:o},{:d},{:x},{:X}, {:%}".format(15, 15, 15, 15, 15, 15.87623, 2)
 
tpl = "numbers: {:b},{:o},{:d},{:x},{:X}, {:%}".format(15, 15, 15, 15, 15, 15.87623, 2)
 
tpl = "numbers: {0:b},{0:o},{0:d},{0:x},{0:X}, {0:%}".format(15)
 
tpl = "numbers: {num:b},{num:o},{num:d},{num:x},{num:X}, {num:%}".format(num=15)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容