一. 字串的表示
用單引號或雙引號構成字串。
「abc」 \
『def』
表示乙個字串,而「abc」+「def」是兩個字串連線在一起,兩者不一樣。
「「」 「」「中間可以為任意長的字串
二.字串操作
1.大小寫轉換
s.capitalize() #字串s首寫字母大寫
s.lower() #全部變成小寫
s.upper() #全部變成大寫
s.swapcase() #大小寫互換
len(s) #得到字串的大小
2.查詢子串
s.find(substring,[start[,end]]) 找到,返回索引值,找不到,返還-1
s.rfind(substring,[start[,end]]) 反向查詢
s.index(substring,[start[,end]]) 與find()類似,如果找不到substring,就產生乙個
valueerror的異常
s.rindex(substring,[start[,end]]) 反向查詢
s.count(substring,[start[,end]]) 返回找到substring的次數
3.格式化字串
用法 s% tuple表示乙個引數列表,把tuple中的每乙個值用字串表示,表示的格 式有s來確定。
s.ljust(width) 左對齊,如果width比len(s)大,則後面補空格。否則返回s。
s.rjust(width) 右對齊
s.center(width) 居中
s.lstrip() 去掉左邊的空白字元
s.rstrip() 去掉右邊的空白字元
s.lstrip() 去掉兩邊的空白字元
4. 字串的合併和分解
合併:s.join(words)
words是乙個含有字串的tuple或list。join用s作為分隔符將words中的字串連線起 來,合併為乙個字串。
例:>>> 「+」.join([」hello」,」my」,」friedn」])
『hello+my+friedn』
分解:s.split(words)
words是乙個字串,表示分隔符。split的操作和join相反。將s分解為乙個list。
例:>>> 「hello my fried」.split(」 「)
[』hello』, 『my』, 『fried』]
python 字元處理
result url.isalpha 判斷是否是字母 result url.isdigit 判斷是否是數字 result filename.endswith doc 判斷結束字元 result filename.startswith a 判斷開始字元string sdfsdfsdf asdf fin...
python處理字元 常用python字串處理
import re p re.compile r d print p.split one1two2three3four4 output one two three four 4 字串的開頭和結尾的處理 例如查詢乙個檔名以什麼開頭或以什麼結尾 filename trace.h print filena...
Python 字元處處理
需要寫個指令碼,從sql檔案中提取uid,並拼成key,刪除redis中的這個key。sql檔案形式 update table 1 set column 1 9459885 where uid 133333333 update table 1 set column 1 9459886 where u...