1. 程式人生 > >python 字串及列表

python 字串及列表

字串

字串的建立:    '' ; "" ; "''" """

str = ' redhat' ; str = "redhat" ; str = """redhat"""

轉義字元

\n: 代表換行符    \t: 代表tab符 \': 代表單引號本身 \": 代表雙引號本身

字串的型別轉換
str(obj) 將其他型別內容轉換為字串
int(obj) 將字串轉換為為整數
float(obj) 將字串轉換為浮點型
long(obj) 將字串轉換為長整型

字串的特性 : 索引 切片 連線 重複 計算長度 成員操作符(in , not in)

索引    

切片

重複

連線

計算長度

成員操作符

判斷字串的型別

s.capitalize  s.find        s.isspace     s.partition   s.rstrip      s.translate
s.center      s.format      s.istitle     s.replace     s.split       s.upper
s.count       s.index       s.isupper     s.rfind       s.splitlines  s.zfill
s.decode      s.isalnum     s.join        s.rindex      s.startswith  
s.encode      s.isalpha     s.ljust       s.rjust       s.strip       
s.endswith    s.isdigit     s.lower       s.rpartition  s.swapcase    
s.expandtabs  s.islower     s.lstrip      s.rsplit      s.title 

字串開頭結尾的判斷(endswith, startswith)

endswith--多用於查詢指定的檔案格式(.log, .png.....)

startswith---多用於判斷使用的協議(http://, https://, ftp://)

去除空格  left middle right , l/r

列表的操作

append   insert      extend

a=['qwer','asdf']

a.append('tyu') 追加到a的列表末尾

a.insert(1,'dfg') 追加到a中第一個元素的後面

a.extend(a) 追加a到列表a的末尾 這個使用一般在追加多個的時候

修改

檢視

.count 顯示列表指定元素出現的次數

.index 顯示指定元素索引值,如果出現多個顯示最小的,如果不存在就報錯

刪除

.pop(索引值)

反轉列表

.serverse()