99999久久久久久亚洲,欧美人与禽猛交狂配,高清日韩av在线影院,一个人在线高清免费观看,啦啦啦在线视频免费观看www

熱線電話:13121318867

登錄
首頁精彩閱讀python中使用%與.format格式化文本方法解析
python中使用%與.format格式化文本方法解析
2018-04-26
收藏

python中使用%與.format格式化文本方法解析

初學(xué)python,看來零零碎碎的格式化文本的方法,總結(jié)一下python中格式化文本的方法。使用不當(dāng)?shù)牡貧g迎指出謝謝。
1、首先看使用%格式化文本
常見的占位符:    
常見的占位符有:
%d  整數(shù)
%f  浮點數(shù)
%s  字符串
%x  十六進制整數(shù)
使用方法:    
>>> 'Hello, %s' % 'world'
'Hello, world'
>>> 'Hi, %s, you have $%d.' % ('Michael', 1000000)
'Hi, Michael, you have $1000000.'
使用的時候不知道寫什么的地方直接使用 %s 進行代替,語句的末尾加上 %() 括號里面直接填寫內(nèi)容即可(字符串加上引號,中間用“,”分割),如果只有一個%?,括號可以省略。
高級一點的用法:
格式化整數(shù)指定是否補零:
首先看代碼:
?    
>>> '%d-%d' % (3, 23)
'3-23'
>>> '%2d-%2d' % (3, 23)
' 3-23'
>>> '%3d-%3d' % (3, 23)
' 3- 23'
>>> '%4d-%4d' % (3, 23)
'  3- 23'
>>> '%01d-%01d' % (3, 23)
'3-23'
>>> '%02d-%02d' % (3, 23)
'03-23'
>>> '%03d-%03d' % (3, 23)
'003-023'
>>> '%04d-%04d' % (3, 23)
'0003-0023'
>>>
可以看得出來,d前面的數(shù)字用來指定占位符,表示被格式化的數(shù)值占用的位置數(shù)量(字節(jié)還是什么不知道這樣的表述是否正確),指定之后比如%3d,代表這個整數(shù)要占用3個位置,前面如果有0代表占用的地方使用0補齊,沒有就使用空格補齊。指定的空間位置小于實際的數(shù)字大小,以實際占用的位置大小為準(zhǔn)。
指定小數(shù)的位數(shù):    
>>> '%.f' % 3.1415926'3'
>>> '%.1f' % 3.1415926'3.1'
>>> '%.2f' % 3.1415926'3.14'
>>> '%.3f' % 3.1415926'3.142'
>>>
可以看出.后面的數(shù)字用來表示保留的小數(shù)點的位數(shù),".1"代表保留小數(shù)點后面一位小數(shù)。
如果不確定應(yīng)該用什么,%s永遠(yuǎn)起作用,它會把任何數(shù)據(jù)類型轉(zhuǎn)換為字符串:    
>>> 'Age: %s. Gender: %s' % (25, True)
'Age: 25. Gender: True'

有些時候,字符串里面的%是一個普通字符怎么辦?這個時候就需要轉(zhuǎn)義,用%%來表示一個%:
    
>>> 'growth rate: %d %%' % 7
'growth rate: 7 %'
嘗試使用其他方法對%進行轉(zhuǎn)義,但是好像沒有用,有什么其他方法歡迎評論。
2、使用format 方法進行格式化
代碼演示:    
age = 25
name = 'Swaroop'
print('{0} is {1} years old'.format(name, age))
print('Why is {0} playing with that python?'.format(name))
位置使用{1}按照使用的順序?qū)懞茫竺娓袷绞褂?.format() 寫好對應(yīng)的參數(shù)即可。
輸出結(jié)果:    
Swaroop is 25 years old
Why is Swaroop playing with that python?
其實也可以使用第一種方法實現(xiàn):    
age = 25
name = 'Swaroop'
print('%s is %s years old'%(name, age))
print('Why is %s playing with that python?'%(name))
輸出    
Swaroop is 25 years old
Why is Swaroop playing with that python?
實現(xiàn)的結(jié)果都是一樣的。
總結(jié)
以上就是本文關(guān)于python中使用%與.format格式化文本方法解析的全部內(nèi)容,希望對大家有所幫助。

數(shù)據(jù)分析咨詢請掃描二維碼

若不方便掃碼,搜微信號:CDAshujufenxi

數(shù)據(jù)分析師資訊
更多

OK
客服在線
立即咨詢
客服在線
立即咨詢
') } function initGt() { var handler = function (captchaObj) { captchaObj.appendTo('#captcha'); captchaObj.onReady(function () { $("#wait").hide(); }).onSuccess(function(){ $('.getcheckcode').removeClass('dis'); $('.getcheckcode').trigger('click'); }); window.captchaObj = captchaObj; }; $('#captcha').show(); $.ajax({ url: "/login/gtstart?t=" + (new Date()).getTime(), // 加隨機數(shù)防止緩存 type: "get", dataType: "json", success: function (data) { $('#text').hide(); $('#wait').show(); // 調(diào)用 initGeetest 進行初始化 // 參數(shù)1:配置參數(shù) // 參數(shù)2:回調(diào),回調(diào)的第一個參數(shù)驗證碼對象,之后可以使用它調(diào)用相應(yīng)的接口 initGeetest({ // 以下 4 個配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺檢測極驗服務(wù)器是否宕機 new_captcha: data.new_captcha, // 用于宕機時表示是新驗證碼的宕機 product: "float", // 產(chǎn)品形式,包括:float,popup width: "280px", https: true // 更多配置參數(shù)說明請參見:http://docs.geetest.com/install/client/web-front/ }, handler); } }); } function codeCutdown() { if(_wait == 0){ //倒計時完成 $(".getcheckcode").removeClass('dis').html("重新獲取"); }else{ $(".getcheckcode").addClass('dis').html("重新獲取("+_wait+"s)"); _wait--; setTimeout(function () { codeCutdown(); },1000); } } function inputValidate(ele,telInput) { var oInput = ele; var inputVal = oInput.val(); var oType = ele.attr('data-type'); var oEtag = $('#etag').val(); var oErr = oInput.closest('.form_box').next('.err_txt'); var empTxt = '請輸入'+oInput.attr('placeholder')+'!'; var errTxt = '請輸入正確的'+oInput.attr('placeholder')+'!'; var pattern; if(inputVal==""){ if(!telInput){ errFun(oErr,empTxt); } return false; }else { switch (oType){ case 'login_mobile': pattern = /^1[3456789]\d{9}$/; if(inputVal.length==11) { $.ajax({ url: '/login/checkmobile', type: "post", dataType: "json", data: { mobile: inputVal, etag: oEtag, page_ur: window.location.href, page_referer: document.referrer }, success: function (data) { } }); } break; case 'login_yzm': pattern = /^\d{6}$/; break; } if(oType=='login_mobile'){ } if(!!validateFun(pattern,inputVal)){ errFun(oErr,'') if(telInput){ $('.getcheckcode').removeClass('dis'); } }else { if(!telInput) { errFun(oErr, errTxt); }else { $('.getcheckcode').addClass('dis'); } return false; } } return true; } function errFun(obj,msg) { obj.html(msg); if(msg==''){ $('.login_submit').removeClass('dis'); }else { $('.login_submit').addClass('dis'); } } function validateFun(pat,val) { return pat.test(val); }