Python(8)--字串方法
center(width[, fillchar]) 指定字串長度,在兩邊填充字元(預設空格),讓字串居中
>>> "hello".center(30) ' hello ' >>> "hello".center(30, "#") '############hello#############'
ljust(width,[, fillchar]) 指定字串長度,在兩邊填充字元(預設空格),讓字串左對齊
>>> "hello".ljust(30) 'hello ' >>> "hello".ljust(30, "#") 'hello#########################'
rjust(width,[, fillchar]) 指定字串長度,在兩邊填充字元(預設空格),讓字串右對齊
>>> "hello".rjust(30) ' hello' >>> "hello".rjust(30, "#") '#########################hello'
zfill(width) 指定字串長度,在左邊填充0,將+或者-移動到開頭
>>> "+3.14".zfill(+30) '+00000000000000000000000003.14' >>> >>> "+3.14".zfill(30) '+00000000000000000000000003.14' >>> "-3.14".zfill(30) '-00000000000000000000000003.14' >>> "hello".zfill(30) '0000000000000000000000000hello'
find(sub[, start[, end]]) 查詢字串中的子串,可以指定查詢範圍,找到就返回第一個子串的索引,沒找到返回-1
>>> "hellohiworldhi".find("hi") 5 >>> "hellohiworldhi".find("hi", 7, 14) 12 >>> "hellohiworldhi".find("hi", 7, 8) -1
rfind(sub[, start[, end]]) 同find,但找到後返回的是最後一個子串的索引
>>> "hellohiworldhi".rfind("hi") 12
index(sub[, start[, end]]) 查詢字串中的子串,可以指定查詢範圍,找到就返回第一個子串的索引,沒找到引發ValueErroe異常
>>> "hellohiworldhi".index("hi") 5 >>> "hellohiworldhi".index("hi", 7, 8) Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: substring not found
rindex(sub[, start[, end]]) 同index,但找到後返回的是最後一個子串的索引
>>> "hellohiworldhi".rindex("hi") 12
count(sub[, start[, end]]) 計運算元串sub出現的次數,可以指定查詢範圍
>>> "hellohiworldhi".count("hi") 2 >>> "hellohiworldhi".count("hi", 7, 8) 0
startswith(prefix[, start[, end]]) 檢查字串是否以perfix開頭,可以指定查詢範圍
>>> "hellohiworldhi".startswith("hi") False >>> "hellohiworldhi".startswith("h") True >>> "hellohiworldhi".startswith("h", 5) True
endswith(suffix[, start[, end]]) 檢查字串是否以suffix結尾,可以指定查詢範圍
join(seq) 將字串與序列seq合併,返回結果
>>> "+".join("hello") 'h+e+l+l+o' >>> >>> seq = "C","admin","bin","test" >>> "/".join(seq) 'C/admin/bin/test'
split([sep[, maxsplit]]) 以sep為分隔符將字串進行分割,得到列表,預設是空白分隔,可以指定最大分隔次數
>>> "a b c d e f".split() ['a', 'b', 'c', 'd', 'e', 'f'] >>> "a b c d e f".split(" ",2) ['a', 'b', 'c d e f'] >>> "1,2,3,4,5".split(",") ['1', '2', '3', '4', '5']
rsplit([sep[, maxsplit]]) 與split相同,從右往左進行分割
lower() 將字串字母變小寫,返回結果
>>> "Hello WorlD".lower() 'hello world'
title() 將字串中所有單詞首字母大寫,返回結果
>>> "helloworld".title() 'Helloworld' >>> "hello world".title() 'Hello World'
upper() 將字串中所有字母變大寫,返回結果
>>> "hello".upper() 'HELLO'
maketrans(x[, y[, z]]) 靜態方法,建立一個供translate使用的轉換表,只傳x,x必須是從字元或者序列到Unicode序數或者None(使用者刪除)的對映;也可以使用兩個表示源字元和目標字元的字串;第三個引數用於指定要刪除的字元
translate(table) 根據轉換表table(通過maketrans建立)對字串中的所有字元進行轉換,返回結果
# 兩個引數是長度相同的字串,指定要將第一個字串的每隔字元都替換為第二個字串中的相應字元 >>> table = str.maketrans("cv", "as") # 轉換表的內容是Unicode碼點的對映 >>> table {99: 97, 118: 115} # 將表作為translate的引數 >>> "hello cv !!".translate(table) 'hello as !!' # 新增第三個引數表示要刪除的字元,這裡新增空格,將空格都刪除 >>> table = str.maketrans("cv", "as", " ") >>> "hello cv !!".translate(table) 'helloas!!'
capitalize() 返回字串的副本,當地一個字元大寫
>>> "hello".capitalize() 'Hello'
casefold() 返回經過標準化後的字串,類似於轉換為寫哦啊寫,更適用於Unicode字串進行不區分大小寫的比較
>>> "HeLLO".casefold() 'hello'
swapcase() 將字串的所有字母大小寫反轉。返回結果
>>> "HeLLO".swapcase() 'hEllo'
replace(old, new[, max]) 將字串中的子串old替換為new,可以指定替換次數,返回結果
>>> "HeLLO".replace("L", "&") 'He&&O' >>> "HeLLO".replace("L", "&", 1) 'He&LO'
strip([chars]) 將字串開頭和結尾的所有指定的字元都刪除(預設是所有空白字元)
>>> " hello ".strip() 'hello' >>> "####hello##".strip("#") 'hello'
lstrip([chars]) 同strip,刪除的是開頭字元
rstrpi([chars]) 同strip,刪除的是結束字元
判斷字串是否滿足特定條件:
isalnum() 檢查字串的字元是否都是字母或數字
isalpha() 檢查字串的字元是否都是字母
isdecimal() 檢查字串的字元是否都是十進位制數
isdigit() 檢查字串的字元是否都是數字
isidentifier() 檢查字串是否用作Python識別符號
islower() 檢查字串中字母是否都是小寫
isnumeric() 檢查字串的字元是否都是數字字元
isprintable() 檢查字串的字元是否都是可以列印的
isspace() 檢查字串的字元是否都是空白字元
istitle() 檢查字串非字母后面單詞首字母是否都是大寫,其他字母小寫
isupper() 檢查字串中字母是否都是大寫