字符格式化,有兩種方式:
1盛险、通過%占位符方式,%s,%d,%
2蕴纳、通過format,其中format比較好用,可以居中、可以用%续徽、可以用二進(jìn)制、可以填充字符自定義亲澡;
1钦扭、利用%的案例
tp1="i am %s"%"aaa"#
tp2="i am %s age %d"%("alex",18)#順序關(guān)聯(lián)
tp3="i am %(name)s age %(age)d"%{"name":"alex","age":18}#指定名稱,起名字
tp4="percent%.2f"%99.567#保留小數(shù)點(diǎn)幾位
tp5="i am %(pp).2f"%{"pp":12.45667,}#指定名稱床绪,保留兩位小數(shù)
tp6="i am %(pp).2f%%"%{"pp":13.34566,}#用雙%%來引用%
print("tp1:",tp1)
print("tp2:",tp2)
print("tp3:",tp3)
print("tp4:",tp4)
print("tp5:",tp5)
print("tp6:",tp6)
執(zhí)行結(jié)果:
2客情、利用format
tp1="i am {},age{},you are{}".format("hhh",123,"yyy")#順序填充
tp2="i am {},age{},you are{}".format(*["hhh",123,"yyy"])#動(dòng)態(tài)參數(shù)填充
tp3="i am {0},age{1},you are{0} too".format("hhh",123)#占位符索引填充,順序填充
tp4="i am {0},age{1},you are{0} too".format(*["hhh",123])#占位符索引填充癞己,動(dòng)態(tài)參數(shù)填充
tp5="i am {name},age{age},you are{name} too".format(name="hhh",age=123)#指定名稱填充膀斋,名稱順序可變
tp6="i am {name},age{age},you are{name} too".format(**{"name":"hhh","age":123})#指定名稱,動(dòng)態(tài)參數(shù)痹雅,字典需要**
tp7="i am {0[0]},age{0[1]},you are{0[2]}".format([1,2,3],[11,22,33])#通過列表傳遞
tp8="i am {:s},age{:d},money{:f}".format("hh",18,88.11)#格式化字符仰担,S字符,d整數(shù)绩社,f浮點(diǎn)型
tp9="i am {name:s},age{age:d}".format(name="hh",age=18)#指定名稱摔蓝,S字符,d整數(shù)愉耙,f浮點(diǎn)型
tp10="i am {name:s},age{age:d}".format(**{"name":"hhh","age":123})#動(dòng)態(tài)參數(shù)+指定名稱贮尉,S字符,d整數(shù)朴沿,f浮點(diǎn)型
tp11="numbers:{:b},{:o},{:d},{:x},{:X},{:%}".format(15,15,15,15,15,3.666)#格式化字符猜谚,b二進(jìn)制,d整型
tp12="numbers:{0:b},{0:o},{0:d},{0:x},{0:X},{0:%}".format(15)#格式化+索引悯仙,b是字節(jié)型龄毡,o是八進(jìn)制,x是16進(jìn)制
tp13="numbers:{num:b},{num:o},{num:d},{num:x},{num:X},{num:%}".format(num=15)#格式化+指定名稱
執(zhí)行結(jié)果:
tp1: i am hhh,age123,you areyyy
tp2: i am hhh,age123,you areyyy
tp3: i am hhh,age123,you arehhh too
tp4: i am hhh,age123,you arehhh too
tp5: i am hhh,age123,you arehhh too
tp6: i am hhh,age123,you arehhh too
tp7: i am 1,age2,you are3
tp8: i am hh,age18,money88.110000
tp9: i am hh,age18
tp10: i am hhh,age123
tp11: numbers:1111,17,15,f,F,366.600000%
tp12: numbers:1111,17,15,f,F,1500.000000%
tp13: numbers:1111,17,15,f,F,1500.000000%
顏色格式:
格式: echo "\033[字背景顏色;字體顏色m輸入的內(nèi)容\033[0m"?
案例:echo "\033[41;36m write something here \033[0m" 锡垄,其中41的位置代表底色, 36的位置是代表字的顏色?
那些ascii code 是對(duì)顏色調(diào)用的始末.?\033[ ; m …… \033[0m?
案例:
a1=input("\033[41;36m write something here \033[0m")#前景色和背景色均設(shè)置
a1=input("\033[41;1m write something here \033[0m")#只設(shè)置背景色沦零,且加粗
a1=input("\033[36;1m write something here \033[0m")#可以單獨(dú)識(shí)別只設(shè)置字體顏色,且加粗
a1=input("\033[36;m write something here \033[0m")#可以單獨(dú)識(shí)別只設(shè)置字體顏色货岭,不加粗
字背景顏色范圍:40----49?
40:黑?
41:深紅?
42:綠?
43:黃色?
44:藍(lán)色?
45:紫色?
46:深綠?
47:白色?
字顏色:30-----------39
30:黑
31:紅
32:綠
33:黃
34:藍(lán)色
35:紫色
36:深綠
37:白色
ANSI控制碼的說明?
\33[0m 關(guān)閉所有屬性
\33[1m 設(shè)置高亮度
\33[4m 下劃線
\33[5m 閃爍
\33[7m 反顯
\33[8m 消隱
\33[30m -- \33[37m 設(shè)置前景色
\33[40m -- \33[47m 設(shè)置背景色
\33[nA 光標(biāo)上移n行
\33[nB 光標(biāo)下移n行
\33[nC 光標(biāo)右移n行
\33[nD 光標(biāo)左移n行
\33[y;xH設(shè)置光標(biāo)位置
\33[2J 清屏
\33[K 清除從光標(biāo)到行尾的內(nèi)容
\33[s 保存光標(biāo)位置
\33[u 恢復(fù)光標(biāo)位置
\33[?25l 隱藏光標(biāo)
\33[?25h 顯示光標(biāo)??