亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

python字符串格式化(%格式符和format方式)

 更新時(shí)間:2022年02月11日 14:29:09   作者:穆梓先生  
在編寫程序的過程中,經(jīng)常需要進(jìn)行格式化輸出,每次用每次查,干脆就在這里整理一下,下面這篇文章主要給大家介紹了關(guān)于python字符串格式化的相關(guān)資料,分別是%格式符和format方式,需要的朋友可以參考下

Python的字符串格式化有兩種方式:%格式符方式,format方式

%格式符

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

  • (name)      可選,用于選擇指定的key
  • flags          可選,可供選擇的值有:
    • +       右對(duì)齊;正數(shù)前加正好,負(fù)數(shù)前加負(fù)號(hào);
    • -        左對(duì)齊;正數(shù)前無符號(hào),負(fù)數(shù)前加負(fù)號(hào);
    • 空格    右對(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 <= 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ù)法表示(超過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)制表示的方式

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

a = "%(name)s-----%(age)d "%{'name':'xx','age':20}
print(a)

執(zhí)行結(jié)果:

xx-----20

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

  • +       右對(duì)齊;正數(shù)前加正好,負(fù)數(shù)前加負(fù)號(hào);
  • -        左對(duì)齊;正數(shù)前無符號(hào),負(fù)數(shù)前加負(fù)號(hào);
  • 空格    右對(duì)齊;正數(shù)前加空格,負(fù)數(shù)前加負(fù)號(hào);
  • 0        右對(duì)齊;正數(shù)前無符號(hào),負(fù)數(shù)前加負(fù)號(hào);用0填充空白處

width         可選,占有寬度

name占10位,+,右對(duì)齊,age占10位,-,左對(duì)齊

b = "%(name)+10s————————%(age)-10d————————"%{'name':'xx','age':20}
print(b)

執(zhí)行結(jié)果:

xx————————20        ————————

 空格,右對(duì)齊

 0,用0填充空白處

c = "------%(year) d******%(age)010d "%{'year':2016,'age':-20}
print(c)

執(zhí)行結(jié)果:

------ 2016******-000000020

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

只保留兩位小數(shù)

d = '--------%(p).2f'%{'p':1.23456}
d1 = '--------%(p)f'%{'p':1.23456}
print(d)
print(d1)

執(zhí)行結(jié)果:

--------1.23
--------1.234560

typecode    必選

  • 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)制表示,并將其格式化到指定位置
e ='***%c***%o***%x'%(65,15,15)
print(e)

執(zhí)行結(jié)果:

***A***17***f

  • e,將整數(shù)、浮點(diǎn)數(shù)轉(zhuǎn)換成科學(xué)計(jì)數(shù)法,并將其格式化到指定位置(小寫e)
  • E,將整數(shù)、浮點(diǎn)數(shù)轉(zhuǎn)換成科學(xué)計(jì)數(shù)法,并將其格式化到指定位置(大寫E)
f = '-----%(num)e------%(num)E'%{'num':1000000000}
print(f)

執(zhí)行結(jié)果:

-----1.000000e+09------1.000000E+09

  • 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;)
g = '-----%(num)g------%(num1)G'%{'num':1000000000,'num1':100}
print(g)

執(zhí)行結(jié)果:

-----1e+09------100

  • %,當(dāng)字符串中存在格式化標(biāo)志時(shí),需要用 %%表示一個(gè)百分號(hào)(類似于轉(zhuǎn)意效果)
s = 'aaa %'
print(s)
s1 = 'aaa %s %%'%('bbb')
print(s1)

執(zhí)行結(jié)果:

aaa %
aaa bbb %

format方式

數(shù)字格式的定義以 ':' 號(hào)開始。碰到了': '字符就知道要定義一個(gè)數(shù)字的顯示格式了。格式的定義順序?yàn)?nbsp;

[[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位)

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)容居中

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

s1 ='---{:*^20s}----'.format('welcome')
print(s1)
s2 ='---{:*>20s}----'.format('welcome')
print(s2)
s3 ='---{:*<20s}----'.format('welcome')
print(s3)

執(zhí)行結(jié)果:

---******welcome*******----
---*************welcome----
---welcome*************----

#            【可選】對(duì)于二進(jìn)制、八進(jìn)制、十六進(jìn)制,如果加上#,會(huì)顯示 0b/0o/0x,否則不顯示

  • 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)

三種方法表示

a1 = "numbers: {:b},{:o},{:d},{:x},{:X}, {:%},{:c}".format(15, 15, 15, 15, 15, 15.87623,65)
a2 = "numbers: {0:b},{0:o},{0:d},{0:x},{0:X}, {0:%},{1:c}".format(15,65)
a3 = "numbers: {num:b},{num:o},{num:d},{num:x},{num:X}, {num:%},{cc:c}".format(num=15,cc=65)
print(a1)
print(a2)
print(a3)

執(zhí)行結(jié)果:

numbers: 1111,17,15,f,F, 1587.623000%,A
numbers: 1111,17,15,f,F, 1500.000000%,A
numbers: 1111,17,15,f,F, 1500.000000%,A

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

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

n = '---{:,d}----'.format(10000000)
n1 = '---{:.2f}----'.format(1.2345)
print(n)
print(n1)

執(zhí)行結(jié)果:

---10,000,000----
---1.23----

 format常用格式化

tp1 = "i am {}, age {}, {}".format("seven", 18, 'alex')
tp2 = "i am {}, age {}, {}".format(*["seven", 18, 'alex'])
tp3 = "i am {0}, age {1}, really {0}".format("seven", 18)
tp4 = "i am {0}, age {1}, really {0}".format(*["seven", 18])
tp5 = "i am {name}, age {age}, really {name}".format(name="seven", age=18)
tp6 = "i am {name}, age {age}, really {name}".format(**{"name": "seven", "age": 18})
tp7 = "i am {0[0]}, age {0[1]}, really {0[2]}".format([1, 2, 3], [11, 22, 33])
tp8 = "i am {:s}, age {:d}, money {:f}".format("seven", 18, 88888.1)
tp9 = "i am {:s}, age {:d}".format(*["seven", 18])
tp10 = "i am {name:s}, age {age:d}".format(name="seven", age=18)
tp11 = "i am {name:s}, age {age:d}".format(**{"name": "seven", "age": 18})
print(tp1)
print(tp2)
print(tp3)
print(tp4)
print(tp5)
print(tp6)
print(tp7)
print(tp8)
print(tp9)
print(tp10)
print(tp11)

執(zhí)行結(jié)果:

i am seven, age 18, alex
i am seven, age 18, alex
i am seven, age 18, really seven
i am seven, age 18, really seven
i am seven, age 18, really seven
i am seven, age 18, really seven
i am 1, age 2, really 3
i am seven, age 18, money 88888.100000
i am seven, age 18
i am seven, age 18
i am seven, age 18

總結(jié)

到此這篇關(guān)于python字符串格式化的文章就介紹到這了,更多相關(guān)python字符串格式化內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論