日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > python >内容正文

python

Python-字符串格式化

發(fā)布時(shí)間:2024/9/30 python 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python-字符串格式化 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

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

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

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

  • flags 可選,可供選擇的值有:
    右對(duì)齊;正數(shù)前加正好,負(fù)數(shù)前加負(fù)號(hào);
    左對(duì)齊;正數(shù)前無(wú)符號(hào),負(fù)數(shù)前加負(fù)號(hào);
    空格 右對(duì)齊;正數(shù)前加空格,負(fù)數(shù)前加負(fù)號(hào);
    0 右對(duì)齊;正數(shù)前無(wú)符號(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 <= 1114111(py27則只支持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ù)法表示(超過(guò)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ù)法表示(超過(guò)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)制表示的方式

常用格式化:

--------------------------------------------------------------------注:如果你對(duì)python感興趣,我這有個(gè)學(xué)習(xí)Python基地,里面有很多學(xué)習(xí)資料,感興趣的+Q群:895817687--------------------------------------------------------------------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.97623tpl = "i am %(pp).2f" % {"pp": 123.425556, }tpl = "i am %.2f %%" % {"pp": 123.425556, }

二、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 【可選】有無(wú)符號(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)

總結(jié)

以上是生活随笔為你收集整理的Python-字符串格式化的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。