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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

2018年12月7日 字符串格式化2 format与函数1

發(fā)布時間:2024/4/15 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 2018年12月7日 字符串格式化2 format与函数1 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
tp7="i am \033[44;1m %(name)-25.6s\033[0m"%{"name":"sxj2343333"} print(tp7) #-為左對齊,\033[44;1m \033[0m 為選取44色號的顏色

?

tp1="I am {},age{},{}" tp2=tp1.format("sxj",18,"abc") print (tp2)tp3="I am {1},age{1},{1}" tp4=tp3.format("sxj",18,"abc") print(tp4)

?

?

2、Format方式

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

    • fill ? ? ? ? ? 【可選】空白處填充的字符
    • align ? ? ? ?【可選】對齊方式(需配合width使用)
      • <,內(nèi)容左對齊
      • >,內(nèi)容右對齊(默認(rèn))
      • =,內(nèi)容右對齊,將符號放置在填充字符的左側(cè),且只對數(shù)字類型有效。 即使:符號+填充物+數(shù)字
      • ^,內(nèi)容居中
    • sign ? ? ? ? 【可選】有無符號數(shù)字
      • +,正號加正,負(fù)號加負(fù);
      • ?-,正號不變,負(fù)號加負(fù);
      • 空格 ,正號空格,負(fù)號加負(fù);
    • # ? ? ? ? ? ?【可選】對于二進(jìn)制、八進(jìn)制、十六進(jìn)制,如果加上#,會顯示 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ù)自動轉(zhuǎn)換成2進(jìn)制表示然后格式化
        • c,將10進(jìn)制整數(shù)自動轉(zhuǎn)換為其對應(yīng)的unicode字符
        • d,十進(jìn)制整數(shù)
        • o,將10進(jìn)制整數(shù)自動轉(zhuǎn)換成8進(jìn)制表示然后格式化;
        • x,將10進(jìn)制整數(shù)自動轉(zhuǎn)換成16進(jìn)制表示然后格式化(小寫x)
        • X,將10進(jìn)制整數(shù)自動轉(zhuǎn)換成16進(jìn)制表示然后格式化(大寫X)
      • 傳入“ 浮點型或小數(shù)類型?”的參數(shù)
        • e, 轉(zhuǎn)換為科學(xué)計數(shù)法(小寫e)表示,然后格式化;
        • E, 轉(zhuǎn)換為科學(xué)計數(shù)法(大寫E)表示,然后格式化;
        • f , 轉(zhuǎn)換為浮點型(默認(rèn)小數(shù)點后保留6位)表示,然后格式化;
        • F, 轉(zhuǎn)換為浮點型(默認(rèn)小數(shù)點后保留6位)表示,然后格式化;
        • g, 自動在e和f中切換
        • G, 自動在E和F中切換
        • %,顯示百分比(默認(rèn)顯示小數(shù)點后6位)
      • tp1="I am {},age{},{}" tp2=tp1.format("sxj",18,"abc") print (tp2)tp3="I am {1},age{1},{1}" tp4=tp3.format("sxj",18,"abc") print(tp4)tp5="I am {name},age{age},{love}" tp6=tp5.format(** {"name":"sxj","age":18,"love":"mm"}) print ("注意傳遞字典需要**加持",tp6)tp5="I am {name},age{age},{love}" tp7=tp5.format(name="sxj",age=111,love="mm") print(tp7)tp5="I am {:s},age{:d},{:.3f}"#用法同%一樣 tp8=tp5.format("sxj",18,44.5225222) print(tp8)tp5="I am {:s},age{:d},{:.3f}" tp9=tp5.format(*["sxj9",11111,22.33433]) print("注意傳遞list用*加持",tp9)tp5="I am {name:s},age{age:d},{love:.3f}" #:s:d:f定義不同類型 tp10=tp5.format(name="abc",age=232,love=89.9545) print(tp10)tpx="numbers:{:b},{:o},{:d},{:x},{:X},{:%}" tp11=tpx.format(12,16,3,40,40,0.33444) print(tp11)







        print("函數(shù)定義")def test(x):" sxj "#函數(shù)定義描述 強(qiáng)烈建議x+=1return print(x) #返回打印值 或者直接返回x test(5)def test2():"abc"for i in range(5):i+=1print (i)return# 可以不寫返回值 test2()print("過程定義:沒有return") print("return 也可以返回多個值")def test3():msg="test3"print(msg)return 1,2,3,4,[2,4,5,4],True# 以元祖的形式返回多個值 t3=test3() print (test3()) # 返回值=0 則返回 none # 返回值=1 返回object # 返回值 > 1 則返回tuple

?

?

def test(x,y,z):print(x)print(y)print(z) test(y=2,x=1,z=4) # 關(guān)鍵字參數(shù)位置無需固定,但不能少 #如果混用,位置參數(shù)必須在關(guān)鍵字參數(shù)左邊 test(4,2,z=5)def handle(x,type="sxj"):#設(shè)置type為默認(rèn)參數(shù)print(x)print(type) handle("hello")#默認(rèn)參數(shù)可以不寫 handle("abc","傳遞默認(rèn)參數(shù)")

?

?

?

# 非固定參數(shù)組: **?字典 *列表/元祖? def test(x,*args):print (x)print (args) test(1,2,3,4,5) #輸出2,3,4,5 組成的元祖 ,args為預(yù)留后續(xù)的可擴(kuò)展 test(1,*['x','y','z']) #前面加*表示遍歷整個列表 test(1,['x','y','z']) #如果沒有*則表示列表作為一個整體def test2(x,**kwargs):print(x)print(kwargs) test2(1,y=2,z=3) #傳遞生成的是字典def test3(x,*args,**kwargs):#代表能接受任何形式的參數(shù)print(x)print(args)print(kwargs) print() test3(1,*[2,3,4],**{"a":2,"b":4,"c":4}) print() test3(1,[4,5,6],a=4,b=4,c=1)

轉(zhuǎn)載于:https://www.cnblogs.com/python1988/p/10081542.html

總結(jié)

以上是生活随笔為你收集整理的2018年12月7日 字符串格式化2 format与函数1的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

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