4.15
六個基本:
1:join(除了字符串 在其他的數(shù)據(jù)類型中也有可能遇到)
text="選擇"
t=‘ ’
v=t.join(text)
print(v)
輸出結(jié)果為: 選 擇
t2=‘_’
v2=t2.join(text)
print(v2)
輸出結(jié)果為:選_擇
2.split:
test="axhskbstabst"
test.split('s')
輸出結(jié)果為:['axh','kb','tab','t']
test.split('s',1)
輸出結(jié)果為:['axh'.'kbstabst']
test.split('s',2)
輸出結(jié)果為:['axh','kb','tabst']
3:find
test="sadhidhacf"
test.find('hi'5,7)【5<=x<7】找序列‘hi’并且找不到則返回-1
4:strip:
test.strip() 去除空格換行水平制表符
test.lsprit()?去除左邊的空格換行水平制表符
test.rsprit()?去除右邊的空格換行水平制表符
5:Upper Lower
test.upper() 轉(zhuǎn)大寫
test.lower() 轉(zhuǎn)小寫
6:replace:
test="ahdudfslnahdf"
test.replace('ah','bb')
運行結(jié)果為:bbdudfslnbbdf
test.replace('ah','bb',1)
運行結(jié)果為:bbdudfslnahdf
四個(在所有數(shù)據(jù)類型中都可用)
1:索引
test="alex"
v=test[3]
print(v)
運行結(jié)果為:x
2:切片
v=test[0,1]【0<=x<1】
[0,-1]【指的是到最后 即想0<=x<4】
3:len:
v=len(test) 【由四個字符組成】
4:for循環(huán)(break和continue支持)
test="堅持"
for 變量名 in test
print(變量名)
運行結(jié)果為:堅
持
一個重點關(guān)注:
字符串一旦創(chuàng)建不可修改 如果拼接或者修改時將重新在內(nèi)存中創(chuàng)建新的變量
注: 幫助創(chuàng)建連續(xù)的數(shù)字 通過設(shè)置步長創(chuàng)建不連續(xù)的數(shù)字
v=range(100) ? v=range(0,100,5)
? for item in v
print(item)
test=input(">>>") 等待用戶輸入 ?sql;
for item in range(0,len(test)):
print(item,test[item])
運行結(jié)果:0 s
1 q
2 l
?
轉(zhuǎn)載于:https://www.cnblogs.com/gl-gl/p/8851211.html
總結(jié)
- 上一篇: lightoj 1037 - Agent
- 下一篇: HALCON示例程序edge_segme