1.前綴和后綴
-
前綴和后綴指的是:字符串是否以指定字符開頭和結尾
2.startswith()
-
判斷字符串是否以指定字符開頭,若是返回True,若不是返回False
str1 = "HelloPython"
print(str1.startswith("Hello")) # True
print(str1.startswith("Python")) # False
3.endswith()
-
判斷字符串是否以指定字符結尾,若是返回True,若不是返回False
str1 = "HelloPython"
print(str1.endswith("thon")) # True
print(str1.endswith("Hello")) # False
4.編碼和解碼
-
字符串編碼和解碼一般用于網絡傳輸數據
5.encode
-
表示字符串以指定字符集進行編碼
str1 = "你好Python"
print(str1.encode("utf-8")) # b'\xe4\xbd\xa0\xe5\xa5\xbdPython'
print(str1.encode("gbk")) # b'\xc4\xe3\xba\xc3Python'
6.decode
-
表示使用指定字符集對數據進行解碼
str2 = b'\xe4\xbd\xa0\xe5\xa5\xbdPython'
str3 = b'\xc4\xe3\xba\xc3Python'
print(str2.decode("utf-8"))
print(str3.decode("gbk"))