第八課 字符串中經常使用的方法:center方法 # 字符串方法:center # 做用是:將字符串在必定的寬度區域內居中顯示 # 這個方法和咱們以前將的format 中的 ^ 同樣 # ^ print("<" + "hello".center(30) + ">") # < hello > # < hello > print("<{:^30}>".format("hello")) # < hello > # print("<" + "hello".center(20, "*") + ">") # <*******hello********> print("<{:*^20}>".format("hello")) # <*******hello********> ------------------------------------------------- 第九課 字符串中經常使用的方法:find方法 在大字符串中來查找子子字符串 若是找到了,find方法就會返回子子字符串的第一個字符在大字符串中出現的位置 有就是索引 若是未找到,那麼find方法就會返回-1 find方法有3個參數 第一個是要查找的子字符串 第二個參數是開始索引 第三個參數是結束索引 左閉右開 # 字符串方法:find s = "hello world" print(s.find("world")) # 6 w的位置索引爲 6 print(s.find("abc")) # -1 不存在返回-1 print(s.find("o")) # 4 返回第一個 0的索引 print(s.find("o",6)) # 7 第二個0 出如今第7個位置 print(s.find("l",5,9)) # -1 print(s.find("l",5,10)) # 9 左閉右開 ------------------------------------------------- 第十課 字符串中經常使用的方法:join方法 # 字符串方法:join # 用於鏈接序列中的元素,split方法的逆方法 list = ["a", "b", "c", "d", "e"] s = '*' print(s.join(list)) # a*b*c*d*e print("xy".join(list)) # axybxycxydxye # C:\abc\xyz # /abc/xyz # c:\usr\local\nginx\ # /usr/local/nginx/ # 這個join 在咱們 linux 和windows 中頗有幫助 不一樣的操做系統的路徑用python代碼如何表示: 要表示一個 linux環境的 路徑 # 先定義一個元組 dirs = ('','usr','local','nginx','') linuxPath = '/'.join(dirs) print(linuxPath) # /usr/local/nginx/ windowPath = ('C:' + '\\'.join(dirs)) print(windowPath) # C:\usr\local\nginx\ #numList = [1,2,3,4,5,6] # 這個跑出異常 必需要轉化成字符串 #print("a".join(numList)) ------------------------------------------------------- 第十一課 字符串中經常使用的方法:split方法 拆開以後爲列表 # 字符串方法:split方法 拆分紅列表 s1 = "a b c d e f" print(s1.split()) # ['a', 'b', 'c', 'd', 'e', 'f'] s2= "a*b*c*d*e" print(s2.split("*")) # ['a', 'b', 'c', 'd', 'e'] path = "/usr/local/nginx" pathList = path.split('/') print(pathList) # ['', 'usr', 'local', 'nginx'] windowPath = "D:" + "\\".join(pathList) print(windowPath) # D:\usr\local\nginx -------------------------------------------------- 第十二課 字符串中經常使用的方法: lower、upper和capwords函數 lower: 把全部的英文字母變成小寫 upper:把全部的英文字母變成大寫 capwords函數:會將獨立的字符串中首字母大寫 # 字符串方法:lower、upper和capwords函數 print("HEllo".lower()) # hello print("hello".upper()) # HELLO list = ["Python", "Ruby", "Java","KOTLIN"] if "Kotlin" in list: print("找到Kotlin了") else: print("未找到Kotlin") # 未找到Kotlin 對大小寫敏感 for lang in list: # 循環遍歷一遍 這樣把每個單詞都轉化爲小寫 if "kotlin" == lang.lower(): print("找到Kotlin了") # 找到Kotlin了 break; from string import capwords s = "i not only like Python, but also like Kotlin" print(capwords(s)) # I Not Only Like Python, But Also Like Kotlin --------------------------------------------------- 第十三課 字符串中經常使用的方法: replace方法和strip方法 replace方法:將一個字符串中出現的一樣的子字符串 替換 strip方法:截取先後空格 # 字符串方法:replace和strip s = "abcdaedf" print(s.replace("a", "12345")) # 12345bcd12345edf print(s.replace("xyz","aa")) # abcdaedf 不存在的話 就返回原字符串 print(" geekori.com ".strip()) # geekori.com print(" < geekori.com > ".strip()) # < geekori.com > 只會截取先後的空格,不會截取 中間的空格 langList = ["python", "java", "ruby", "scala", "perl"] lang = " python " if lang in langList: print("找到了python") else: print("未找到python") # 未找到python if lang.strip() in langList: print("找到了python") else: print("未找到python") # 找到了python s = "*** $$* Hello * World ***$$$ " print(s.strip(" *$")) # Hello * World " *$" 這裏面的三個是或的關係 ------------------------------------------------- 第十四課 字符串中經常使用的方法:translate方法和maketrans方法 # 字符串方法:translate和maketrans # translate:替換單個字符 maketrans方法:轉化爲字典 s = "I not only like python, but also like kotlin." table = s.maketrans("ak", "*$") # 他的這個意思是把a替換爲* k替換爲 $ print(table) # {97: 42, 107: 36} 先轉發爲字典 ACLLZ嗎值 print(s.translate(table)) # I not only li$e python, but *lso li$e $otlin. table1 = s.maketrans("ak", "*$", " ") # 第三個參數 刪除的意思 總體的意思是: 把a 轉化爲 * ,k 轉化爲 $ 並刪除掉中間的空格 print(table1) # {97: 42, 107: 36, 32: None} print(s.translate(table1)) # Inotonlyli$epython,but*lsoli$e$otlin. # 把全部的 空格後刪除了