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

熱線電話:13121318867

登錄
首頁精彩閱讀Python字符串拼接、截取及替換方法總結分析
Python字符串拼接、截取及替換方法總結分析
2017-11-18
收藏

Python字符串拼接、截取及替換方法總結分析

本文實例講述了Python字符串拼接、截取及替換方法。分享給大家供大家參考,具體如下:
python字符串連接
python字符串連接有幾種方法,我開始用的第一個方法效率是最低的,后來看了書以后就用了后面的2種效率高的方法,跟大家分享一下。

先介紹下效率比較低的方法:    
a = ['a','b','c','d']
content = ''
for i in a:
content = content + i
print content

content的結果是:'abcd'

后來我看了書以后,發(fā)現(xiàn)書上說的是python連接字符串,特別是在循環(huán)連接字符串的時候,他每次連接一次,就要重新開辟空間,然后把字符串連接起來,再放入新的空間,再一次循環(huán),又要開辟新的空間,把字符串連接起來放入新的空間,如此反復,內存操作比較頻繁,每次都要計算內存空間,然后開辟內存空間,再釋放內存空間,效率非常低,你也許操作比較少的數(shù)據的時候看不出來,感覺影響不大,但是你碰到操作數(shù)據量比較多的時候,這個方法就要退休了,還是看看下面2個比較先進的方法把。

先進方法1:

用字符串的join方法:
    
a = ['a','b','c','d']
content = ''
content = ''.join(a)
print content

content的結果:'abcd'

先進方法2:

用字符串的替換占位符替換    
a = ['a','b','c','d']
content = ''
content = '%s%s%s%s' % tuple(a)
print content

content的結果是:'abcd'

有興趣可以看看關于python字符串有關的 python 字符串替換 和 python 字符串截取

字符串截取

python的字符串是有序集合,我們可以通過索引來提取想要獲取的字符,可以把python的字符串也做為字符串的列表就更好理解。

python的字串列表有2種取值順序

1.是從左到右索引默認0開始的,最大范圍是字符串長度少1

s = 'ilovepython'

s[0]的結果是i

2.是從右到左索引默認-1開始的,最大范圍是字符串開頭

s = 'ilovepython'

s[-1]的結果是n

上面這個是取得一個字符,如果你的實際要取得一斷字符的話,可以用到python 字符串截取功能

比如:s = 'ilovepython'

s[1:5]的結果是love

當使用以冒號分隔的字符串,python返回一個新的對象,結果包含了以這對偏移標識的連續(xù)的內容,左邊的開始是包含了下邊界,比如上面的結果包含了s[1]的值l,而取到的最大范圍不包括上邊界,就是s[5]的值p

字符串替換

python 字符串替換是python操作字符串的時候經常會碰到的問題,這里簡單介紹下字符串替換方法。

python 字符串替換可以用2種方法實現(xiàn):

1.用字符串本身的replace方法
1
    
a.replace('word','python')

輸出的結果是hello python

2.用正則表達式來完成替換:
    
import re
strinfo = re.compile('word')
b = strinfo.sub('python',a)
print b

輸出的結果也是hello python

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

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

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

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(); // 調用 initGeetest 進行初始化 // 參數(shù)1:配置參數(shù) // 參數(shù)2:回調,回調的第一個參數(shù)驗證碼對象,之后可以使用它調用相應的接口 initGeetest({ // 以下 4 個配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺檢測極驗服務器是否宕機 new_captcha: data.new_captcha, // 用于宕機時表示是新驗證碼的宕機 product: "float", // 產品形式,包括: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); }