Python day8常用格式化format類2

2022-02-24 19:57:53 字數 1271 閱讀 6561

format常用格式化
tp1="

i am {},age {},{}

".format('

littlepage

',18,'

boy'

)tp2="

i am {},age {},{}

".format(*['

littlepage

',18,'

boy'])#

tp3='

i am ,age ,really

'.format("

littlepage

",18)#

從索引0開始,一直傳入

tp4='

i am ,age ,really

'.format(name='

littlepage

',age=18)#

建議使用此方法,該方法比較明了

tp5='

i am ,age ,really

'.format(**)#

字典相當於2重求位址,所以加2個*(不懂理解正不正確)

tp6='

i am ,age,not really

'.format(['

littlepage

',18,19],['

largepage

',17,16])#

同樣理解是位址,便於記憶

print(tp6)

其他格式

b將十進位制轉為2進製格式化(bin)

c將十進位制轉為unicode字元格式化(characteristic)

d十進位制整數(decimal)

o八進位制整數(octonary)

x十六進製制小寫(hexadecimal)

x十六進製制大寫

傳入浮點型或者小數類引數

e科學計數法小寫e並格式化

e科學計數法大寫e並格式化

f轉換為浮點型(預設小數點後保留6位)

f轉換為浮點型

g自動在ef切換

f自動在ef切換

%顯示百分比(預設小數點後6位)

舉例:

tp='

numbers:,,,,,,

'.format(2,10,10,16,16,12.2222222222,0.22)

print(tp)

Python day 8 異常處理)

以下是 python 內建異常類的層次結構 baseexception systemexit keyboardinterrupt generatorexit exception stopiteration arithmeticerror floatingpointerror overflowerro...

Python Day8 異常處理

python常見的標準異常總結 異常解釋 assertionerror 斷言語句 assert 失敗 attributeerror 嘗試訪問未知的物件屬性 importerror 匯入模組失敗 indexerror 索引超出序列的範圍 keyerror 字典中查詢乙個不存在的關鍵字 memoryer...

python day8 檔案操作

引數 1.檔案路徑。2.編碼方式,encode 3.執行動作 開啟方式 唯讀,只寫,追加,讀寫,寫讀。f open f mm wpcmimi.txt encoding utf 8 content f.read print content f.close f 變數,f obj,file,f handl...