字符串操作 、原生字符串

字符串的操作,以及常用的函數舉例


1、 對字符串的修改都會產生新串,原串不變
2、原生字符串(正則基礎)
    path = "D:\steam\1appcache"   結果爲:D:\steamappcache 出錯
    path = "D:\\stam\\1appcache"  結果爲:D:\stam\1appcache
    path = r"D:\stam\1appcache"   原生字符串,反斜槓不代表轉義 結果爲:D:\stam\1appcache
3、通用操作
    拼接、重複、成員操作,沒有增刪改操作
4、字符串常用函數
   1)字符串查找和替換
 

str1 = 'a fox jumped over the fence.the dog the pig '
print(str1.find("a", 5, 7))  # find(要檢查的字符串,start=0,end=len(str)) 從左到右查找
# rfind 從右向左查找 返回一個下標, 不存在返回-1
print(str1.index("a", 5, 7))  # 與find用法相同,查詢失敗會報錯,find返回-1
print(str1.count("o", 2, len(str1)))  # count(要檢查的字符串,start=0,end=len(str)) 指定字符串出現的次數
print(str1.replace("the", "an", 2))   # replace(old_str,new_str,count) 用new_str替換old_str,count是替換的次數
   2)字符串分割和組合

print(str1.split(" ", 3))    # split(分隔符,分割次數),分割次數,默認是全部分割
str2 = 'ab c\n\nde fg\rkl\r\n'
print(str2.splitlines(True))       # splitlines(參數) 拆分包含多行的字符串,每行的一個元素返回一個列表。
# 當參數爲非0整數或者True 時,保留行尾標誌,即爲換行符,否則不保留
str3 = ['a','b','c']
str = "+"
print(str.join(str3))    # a+b+c   join 以str爲分隔符,將join括號中所有的元素合併成一個新的字符串

str4 = 'a fox jumped over the fence.the dog the pig '
print(str4.partition("over"))  # partition(分隔符) 結果爲元組,分隔符前面的爲一個元素,分隔符爲一個,分隔符後面的爲一個元素
   3)字符串判斷
    
str1 = 'adasdwe2324'
print(str1.isalpha())  # 判斷字符串是否只由字母構成,是返回True,否則返回False
print(str1.isalnum())  # 判斷字符串是否由數字或字母構成,是返回True,否則返回False
str2 = '12234'
str3 = b'23'
print(str2.isdigit())  # 判斷字符串是否由數字構成,可以判斷byte
print(str3.isdigit())  # True
print(str2.isdecimal())  # 也可以判斷數字,但不能判斷byte
str4 = '二'
print(str4.isnumeric())  # 可以判斷數字和中文數字
str5 = '        '
print(str5.isspace())  # 判斷字符串只有空格和tab組成
str6 = 'qwssdf'
print(str6.islower())  # 判斷字符串中是否都是小寫字母
print(str6.isupper())  # 判斷字符串中是否都是大寫字母
str7 = 'a fox jumped over the fence.the dog the pig'
print(str7.startswith("o",3,10))   # ⽤於判斷字符串是否以指定⼦字符串開頭,如果是則返回True,否則返回False。
print(str7.endswith("g",0,len(str7)))  # ⽤於判斷字符串是否以指定⼦字符串結尾,如果是則返回True,否則返回False
   4)字符串轉換

str1 = 'qwer'
print(str1.upper())  # 小寫轉大寫
str2 = 'ASDW'
print(str2.lower())  # 大寫轉小寫
str3 = 'asdWEREWR'
print(str3.swapcase())  # 小寫轉大寫,大寫轉小寫
print(str3.capitalize())  # 字符串第一個字符轉大寫,其他轉小寫
str4 = 'a fox jumped over the fence.the dog the pig'
print(str4.title())  # 字符串中每個單詞的⾸字⺟⼤寫,其餘⼩寫
str5 = '  dsf  '
print(str5.strip())  # 去除字符串左邊指定的字符,默認是去除空格
print(str5.lstrip())  # 去除字符串左邊指定的字符,默認是去除空格
print(str5.rstrip())  # 去除字符串右邊指定的字符,默認是去除空格
   5)其他方法
    1> str() 將其他類型轉換爲字符串
    2> ord() 返回字符對應的碼值
        print(ord('a'))   # 97
    3> chr() 輸入一個unicode碼,返回一個對應的字符
        print(chr(20013))  # 中

 5、格式化操作
   1)%
   2)format

    把format參數中參數名是a替換{a}, 參數名 = 值
    res = "a={a},b={b},c={c}".format(c=c,a=a,b=b)
    res= f"a={a},b={b},c={c}"  python 3.7

str1 = 'I am {:^5}, age {:>10d}'.format("淺語呀",18)
print(str1)


6、字節字符串  :只能表示ascii(0-255)  用在簽名加密
   1)字符串轉字節
    password = "234" # Python字符串
    res = password.encode("utf8")  # 字節型字符串
    print(type(res))
    res = hashlib.sha1(res).hexdigest()
    print(res)
   2)字節字符串轉Python字符串
    b2 = "hello"
    res = b2.decode("utf8")
    print(res)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章