99热99这里只有精品6国产,亚洲中文字幕在线天天更新,在线观看亚洲精品国产福利片 ,久久久久综合网

歡迎加入QQ討論群258996829
麥子學(xué)院 頭像
蘋果6袋
6
麥子學(xué)院

python字符串格式化的兩種方式詳解

發(fā)布時(shí)間:2016-10-29 23:29  回復(fù):0  查看:3463   最后回復(fù):2016-10-29 23:29  

Python開發(fā)字符串格式化有兩種方式百分號(hào)方式、format方式

  百分號(hào)的方式相對(duì)來說比較老,而format方式則是比較先進(jìn)的方式,企圖替換古老的方式,目前兩者并存。


This PEP proposes a new system for built-in string formatting operations, intended as a replacement for the existing '%' string formatting operator.

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

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

  . (name) 可選,用于選擇指定的key

 ?。?flags 可選,可供選擇的值有:

 ?。?+ 右對(duì)齊;正數(shù)前加正好,負(fù)數(shù)前加負(fù)號(hào);

 ?。?- 左對(duì)齊;正數(shù)前無符號(hào),負(fù)數(shù)前加負(fù)號(hào);

 ?。?nbsp;空格 右對(duì)齊;正數(shù)前加空格,負(fù)數(shù)前加負(fù)號(hào);

 ?。?0 右對(duì)齊;正數(shù)前無符號(hào),負(fù)數(shù)前加負(fù)號(hào);用0填充空白處

 ?。?width 可選,占有寬度

 ?。?.precision 可選,小數(shù)點(diǎn)后保留的位數(shù)

  . typecode 必選

 ?。?s,獲取傳入對(duì)象的__str__方法的返回值,并將其格式化到指定位置

 ?。?r,獲取傳入對(duì)象的__repr__方法的返回值,并將其格式化到指定位置

. c,整數(shù):將數(shù)字轉(zhuǎn)換成其unicode對(duì)應(yīng)的值,10進(jìn)制范圍為 0 <= i <= 1114111py27則只支持0-255);字符:將字符添加到指定位置

 ?。?o,將整數(shù)轉(zhuǎn)換成 八 進(jìn)制表示,并將其格式化到指定位置

 ?。?x,將整數(shù)轉(zhuǎn)換成十六進(jìn)制表示,并將其格式化到指定位置

 ?。?d,將整數(shù)、浮點(diǎn)數(shù)轉(zhuǎn)換成 十 進(jìn)制表示,并將其格式化到指定位置

 ?。?e,將整數(shù)、浮點(diǎn)數(shù)轉(zhuǎn)換成科學(xué)計(jì)數(shù)法,并將其格式化到指定位置(小寫e

 ?。?E,將整數(shù)、浮點(diǎn)數(shù)轉(zhuǎn)換成科學(xué)計(jì)數(shù)法,并將其格式化到指定位置(大寫E

 ?。?f, 將整數(shù)、浮點(diǎn)數(shù)轉(zhuǎn)換成浮點(diǎn)數(shù)表示,并將其格式化到指定位置(默認(rèn)保留小數(shù)點(diǎn)后6位)

 ?。?F,同上

 ?。?g,自動(dòng)調(diào)整將整數(shù)、浮點(diǎn)數(shù)轉(zhuǎn)換成 浮點(diǎn)型或科學(xué)計(jì)數(shù)法表示(超過6位數(shù)用科學(xué)計(jì)數(shù)法),并將其格式化到指定位置(如果是科學(xué)計(jì)數(shù)則是e;)

  . G,自動(dòng)調(diào)整將整數(shù)、浮點(diǎn)數(shù)轉(zhuǎn)換成 浮點(diǎn)型或科學(xué)計(jì)數(shù)法表示(超過6位數(shù)用科學(xué)計(jì)數(shù)法),并將其格式化到指定位置(如果是科學(xué)計(jì)數(shù)則是E;)

  . %,當(dāng)字符串中存在格式化標(biāo)志時(shí),需要用 %%表示一個(gè)百分號(hào)

  注: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, }

  2Format方式

  format 可以居中、%百分號(hào)、二進(jìn)制、數(shù)字加逗號(hào)、填充。而做不到。

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

 ?。?fill 【可選】空白處填充的字符

 ?。?align 【可選】對(duì)齊方式(需配合width使用)

 ?。?<,內(nèi)容左對(duì)齊

 ?。?>,內(nèi)容右對(duì)齊(默認(rèn))

 ?。?nbsp;=,內(nèi)容右對(duì)齊,將符號(hào)放置在填充字符的左側(cè),且只對(duì)數(shù)字類型有效。 即使:符號(hào)+填充物+數(shù)字

  . ^,內(nèi)容居中

  . sign 【可選】有無符號(hào)數(shù)字【可選】對(duì)于二進(jìn)制、八進(jìn)制、十六進(jìn)制,如果加上#,會(huì)顯示 0b/0o/0x,否則不顯示

 ?。?nbsp;+,正號(hào)加正,負(fù)號(hào)加負(fù);

 ?。?nbsp;-,正號(hào)不變,負(fù)號(hào)加負(fù);

 ?。?nbsp;空格 ,正號(hào)空格,負(fù)號(hào)加負(fù);

 ?。?nbsp;, 【可選】為數(shù)字添加分隔符,如:1,000,000

 ?。?width 【可選】格式化位所占寬度

 ?。?.precision 【可選】小數(shù)位保留精度

  . type 【可選】格式化類型

 ?。?nbsp;傳入” 字符串類型 的參數(shù)

 ?。?s,格式化字符串類型數(shù)據(jù)

 ?。?nbsp;空白,未指定類型,則默認(rèn)是None,同s

 ?。?nbsp;傳入“ 整數(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

 ?。?nbsp;傳入“ 浮點(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)在ef中切換

 ?。?G, 自動(dòng)在EF中切換

 ?。?%,顯示百分比(默認(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)

重點(diǎn):

python字符串格式化的兩種方式詳解

文章來源:博客園

您還未登錄,請(qǐng)先登錄

熱門帖子

最新帖子

?