1. 程式人生 > >(三)Python學習之字串常用操作(上)

(三)Python學習之字串常用操作(上)

(三)Python學習之字串常用操作(上)

一、字母處理

  1. upper(): 全部大寫;
  2. lower(): 全部小寫;
  3. swapcase(): 大小寫互換;
  4. capitalize(): 首字母大寫,其餘小寫;
  5. title(): 首字母大寫(轉換為標題);
  6. casefold(): 全部小寫(很多未知的對應變小寫)
test1 = "helloWorld"
print(test1.upper())
print(test1.lower())
print(test1.swapcase())
test2 = "hello World"
print(test2.capitalize())
print(test2.title())

在這裡插入圖片描述

S1 = "HeLLo"
S2 = "ß"  # 德語,"ß"正確的小寫是"ss"
print(S1.lower())
print(S1.casefold())
print(S2.lower())
print(S2.casefold())

在這裡插入圖片描述
二、格式化相關

  1. ljust(width, fillchar = None): 獲取固定長度,左對齊,右邊不夠用fillchar補齊;當fillchar為空,用空格補齊;
  2. rjust(width, fillchar = None): 獲取固定長度,右對齊,左邊不夠用fillchar補齊;當fillchar為空,用空格補齊;
  3. center(width, fillchar = None): 獲取固定長度,中間對齊,兩邊不夠用fillchar補齊;當fillchar為空,用空格補齊;
  4. zfill(width): 獲取固定長度,右對齊,左邊不足用0補齊;
  5. expandtabs(tabsize = None): 把字串中的 tab 符號(’\t’)轉換為tabsize個空格;當tabsize為空時,轉換為0個空格;tab 符號(’\t’)預設的空格數是 8;
a = "11111"
print(a.ljust(10, "*"))
print(a.ljust(10))
print(a.rjust(10, "*"))
print(a.rjust(10))
print(a.center(10, "*"))
print(a.center(10))
print(a.zfill(10))

在這裡插入圖片描述

str = "this is\tstring example....wow!!!"
print(str)
print(str.expandtabs())
print(str.expandtabs(16))

在這裡插入圖片描述
三、字串搜尋相關

  1. find(sub, start = None, end = None): 從左到右在指定範圍(大於等於start小於end)內搜尋指定字串sub,找到第一個之後,返回其位置,沒有返回-1;無指定範圍則表示搜尋整個字串;
  2. index(sub, start = None, end = None): 從左到右在指定範圍(大於等於start小於end)內搜尋指定字串sub,找到第一個之後,返回其位置,沒有程式報錯;無指定範圍則表示搜尋整個字串;
  3. rfind(sub, start = None, end = None):從右到左在指定範圍(大於等於start小於end)內搜尋指定字串sub,找到第一個之後,返回其位置,沒有返回-1;無指定範圍則表示搜尋整個字串;
  4. count(sub,start = None,end = None): 在指定範圍(大於等於start小於end)內統計指定的字串sub出現的次數;無指定範圍則表示搜尋整個字串;
s = "hello world"
#find
print(s.find("e"), s.find("o"), s.find("g"), s.find("o", 4, 8), s.find("o", 8, 10))
#rfind
print(s.rfind("e"), s.rfind("o"), s.rfind("g"), s.rfind("o", 4, 8), s.rfind("o", 8, 10))

在這裡插入圖片描述

s = "hello world"
#index
print(s.index("o"))
print(s.index("o", 6, 8))

在這裡插入圖片描述

s = "hello world"
#index
print(s.index("g"))

在這裡插入圖片描述

s = "hello world"
#count
print(s.count("o"))
print(s.count("o", 3, 5))

在這裡插入圖片描述
四、字串替換

  1. replace(‘old’, ‘new’ , count = None): 從左至右依次替換count次的old為new;當count為空時,整個字串中的old均替換為new;
s = "elelelelelelelele"
print(s.replace("e", "*"))
print(s.replace("e", "*", 3))

在這裡插入圖片描述
五、字串去空格及去指定字元

  1. strip(chars = None): 去除兩邊字元chars;當chars為空時,去除兩邊空格;
  2. lstrip(chars = None): 去除左邊字元chars;當chars為空時,去除左邊空格;
  3. rstrip(chars = None): 去除右邊字元chars;當chars為空時,去除右邊空格;
  4. split(sep = None, maxsplit = None): 按從左至右的前maxspliit個指定字元sep分割字串為陣列;當sep為空時,按空格分隔;當maxsplit為空時,按全部的字元sep分割;(字元sep不包含在結果之中)
  5. rsplit(sep = None, maxsplit = None): 按從右至左的前maxspliit個指定字元sep分割字串為陣列;當sep為空時,按空格分隔;當maxsplit為空時,按全部的字元sep分割;(字元sep不包含在結果之中)
s = "   h e-l lo   "
t = "***h e-l lo***"
#strip,lstrip,rstrip,
print(s)
print(s.strip())
print(s.lstrip())
print(s.rstrip())
print(t)
print(t.strip("*"))
print(t.lstrip("*"))
print(t.rstrip("*"))

在這裡插入圖片描述

s = "abc-de fg-hi jkl-mno-pq"
#split,rsplit
print(s)
print(s.split())
print(s.split("-"))
print(s.split("-", 2))
print(s.rsplit())
print(s.rsplit("-"))
print(s.rsplit("-", 2))

在這裡插入圖片描述
六、字串判斷

  1. endswith(suffix, start=Nome, end=None): 在給定範圍內判斷字串是否以suffix結尾;當給定範圍為空時,判斷整個字串;
  2. startswith(suffix, start=Nome, end=None): 在給定範圍內判斷字串是否以suffix開頭;當給定範圍為空時,判斷整個字串;
  3. isalnum(): 判斷字串中是否只包含字母和數字;
  4. isalpha(): 判斷字串中是否只包含字母和漢字;
  5. isdecimol(): 判斷字串是否為數字;
  6. isdigit(): 判斷字串是否為數字,較isdecimol()範圍更大;
  7. isnumeric(): 判斷字串是否為數字,較isdigit()範圍更大;
  8. isprintable(): 判斷字串是否不存在不可顯示的字元,如\t \n;
  9. isspace(): 判斷字串是否全部都是空格;
  10. istitle(): 判斷字串是否是標題;
  11. isidentifier(): 判斷字串是否是識別符號;
  12. isupper(): 判斷字串是否是全部大寫;
  13. islower(): 判斷字串是否是全部小寫;
test = "ljwan"
#startswith,endswith
print(test.startswith("lj"))
print(test.startswith("lj", 3, 7))
print(test.endswith("an"))
print(test.endswith("an", 1, 2))

在這裡插入圖片描述

test1 = "ljwan520"
test2 = "ljwan520**"
test3 = "lj哈哈哈"
test4 = "lj哈哈哈520"
#isalnum,isalpha
print(test1.isalnum())
print(test2.isalnum())
print(test3.isalpha())
print(test4.isalpha())

在這裡插入圖片描述

#isdecimol,isdigit,isnumeric
num1 = "1"
num2 = "①"
num3 = "四"
print(num1.isdigit(), num1.isdecimal(), num1.isnumeric())
print(num2.isdigit(), num2.isdecimal(), num2.isnumeric())
print(num3.isdigit(), num3.isdecimal(), num3.isnumeric())

在這裡插入圖片描述

test1 = "lj520"
test2 = " lj520"
test3 = "lj\n520"
test4 = "lj\t520"
test5 = "     "
#isprintable,isspace
print(test1.isprintable())
print(test2.isprintable())
print(test3.isprintable())
print(test2.isspace())
print(test5.isspace())

在這裡插入圖片描述

#istitle,isidentifier
test1 = "Lu Jing Wo Ai Ni"
test2 = "Lu jing wo ai ni"
test3 = "LU JING WO AI NI"
test4 = "lj_520"
print(test1.istitle())
print(test2.istitle())
print(test3.istitle())
print(test1.isidentifier())
print(test4.isidentifier())

在這裡插入圖片描述

#isupper,islower
test1 = "Lu Jing Wo Ai Ni"
test2 = "lu jing wo ai ni"
test3 = "LU JING WO AI NI"
print(test1.isupper())
print(test2.isupper())
print(test3.isupper())
print(test1.islower())
print(test2.islower())
print(test3.islower())

在這裡插入圖片描述