重點:字符串的常用函數
#1.測試轉換大小寫 ?
lower:大寫->小寫 ? ?upper:小寫->大寫 ? swapcase:自動將大寫轉小寫小寫轉大寫
print("ABC".lower()) ? ? ? ? ? ?#abcprint("abc".upper()) ? ? ? ? ? ?#ABCprint("aBc".swapcase()) ? ? ? ? #aBc
2.刪除空格:
? ? ? ? strip:刪除兩端空格
? ? ? ? lstrip:刪除左邊的空格
? ? ? ? rstrip:刪除右邊的空格
#2.測試刪除空格
#stripprint(" ? ?zhangsan ? ? ".strip())#lstripprint(" ? ?zhangsan ? ? ".lstrip())#rstripprint(" ? ?zhangsan ? ? ".rstrip())
3.字符串位置
? ? ? ? center:把字符串居中顯示,并且用指定的字符填充在字符串左右。
? ? ? ? ljust:把字符串左對齊,并使用指定字符填充到指定寬度
? ? ? ? rjust:把字符串右對齊,使用指定字符將前面的填充完整
#測試字符串位置問題
#centerprint("lisi".center(50,"-")) ? ? ? ?#-----------------------lisi----------------------- ? lisi 與 - 加起來共為50個字符#ljustprint("lisi".ljust(50,"-")) ? ? ? ? #lisi---------------------------------------------- ?#rjustprint("lisi".rjust(50,"-")) ? ? ? ? #----------------------------------------------lisi
4.檢索字符匹配的索引:
? ? ? ? ? ? find():返回子字符串第一次出現的索引,如果檢索不到返回-1
? ? ? ? ? ? rfind:返回字符串最后一次出現的索引
? ? ? ? ? ? index:和find完全一樣,只是檢索不到會報錯沒有返回值
? ? ? ? ? ? rindex:返回字符串最后一次出現的索引
#find
print("i am wzw".find("w")) ? ? ? #5 ?因為空格也屬于字符串類型print("123456".find("9")) ? ? ? ? #-1 未檢索到輸出-1#rfindprint("i am wzw".rfind("w")) ? ? ? ?#7#index#print("123456".index("9")) ? ? ValueError: substring not found ?未檢索到會報錯
#案例:返回一個域名公司名稱的起始索引 ? ? ? ? ? www.baidu.com
print("www.baidu.com".find(".") + 1) ? ? ? ?#4
5.字符串的替換:
? ? ? ? ? ? replace("將替換的字符","替換后的字符")
#replace
print("i love python".replace("love","愛")) ? ? ? ? #i 愛 python
?
6.字符串的生成
? ? ? ? ? ? join:把可迭代對象(像列表、元組、集合等)中的元素連接成一個字符串,連接時會使用調用 join() 方法的字符串作為分隔符。
print("-".join("help")) ? ? ? ? # h-e-l-p
#案例:將列表中的所有元素通過 - 連接起來
list1 = [10,20,30,40,50]res = str(list1)res = res.replace("[","")res = res.replace("]","")res = res.replace(",","")res = res.replace(" ","")print("-".join(res)) ? ? ? ? ? ?#1-0-2-0-3-0-4-0-5-0 ? 思考如何變成 10-20-30-40-50
7.首字母大寫:
? ? ? ? ? ? capitalize:將首字母大寫
print("python".capitalize()) ? ? ? ?#Python
#count: 統計一個元素在字符串中出現的次數
print("i am wzw".count("w")) ? ? ? ?#2
8.判斷問題(bool類型 ?輸出為 T/F):
? ? ? ? ? ? startswith:檢查字符串是否以指定的前綴開始
? ? ? ? ? ? endswith:檢查字符串是否以指定的后綴結束
print("123wzw".startswith("123")) ? ? ? ? ? #Trueprint("123wzw".endswith("123")) ? ? ? ? ? ? #False
9.分割
? ? ? ? ? ? split("分割對象"):根據指定的分隔符將字符串分割成多個子字符串,并返回一個包含這些子字符串的列表。
'''
print("i am wzw".split(" ")) ? ? ? ? ? ? ? ?#['i', 'am', 'wzw'] ?以空格分割
print("i am wzw".split("w")) ? ? ? ? ? ? ? ?#['i am ', 'z', ''] ?w被切割掉了
#案例:輸入各科成績并使用空格分割
res = input("請輸入各科成績,并使用空格分割:")print(res.split(" ")[0]) ? ?#10print(res.split(" ")[1]) ? ?#20print(res.split(" ")[2]) ? ?#30'''
? ? ? ? 10. partition(子字符串):將一個字符串拆分為三部分,含子字符串本身及左右兩邊,返回一個元組
'''
print("i am wzw".partition("w")) ? ? ? ?#('i am ' , ?'w' , ?'zw')
11.自定義規則替換字符串
? ? ? ? ? ? maketrans: 定義映射規則
? ? ? ? ? ? translate: 根據規則替換字符串
str_a = "abcde"str_b = "12345"rule = str.maketrans(str_a,str_b) ? ? ? ? ? ? ? #定義映射規則print("at bt ct dt et".translate(rule)) ? ? ? ? #1t 2t 3t 4t 5t ? abcde已依次替換為12345
?
# 使用替換轉換表進行轉換
str_c = "abcdef"rule2 = str.maketrans('bdf', '123') ? ? ? ? ? #將bdf替換為123result1 = str_c.translate(rule2)print(result1) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?# a1c2e3
# 使用刪除轉換表進行轉換
rule3 = str.maketrans('', '', 'abc')result2 = str_c.translate(rule3)print(result2) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?# def