天天看點

字元串操作 、原生字元串字元串的操作,以及常用的函數舉例

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

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)