一、字符串
1.什么是字符串(str)
1)字符串
字符串時容器型數據類型(序列); 以單引號或者雙引號作為容器的標志, 引號中所有的內容都屬于字符串的元素
'abc' - > 元素分別是'a', 'b', 'c' 3個元素
'a,b,c' - > 元素分別是'a', ',', 'b', ',', 'c' 5個元素
特點: 不可變, 有序(支持下標操作)
2)字符串的元素
字符串中的元素又叫字符(注意: python中有字符的概念, 但是沒有字符類型; 長度是1的字符串就可以看成字符)
a.普通字符: 字母、數字、各國的文字和符號(可以直接寫在引號中的符號)
'abc', 'abc123', '+-%abc胡說'
b.轉義字符: 在字符串中在一些特定的符號前加\來表示特殊的功能和意義 (算一個字符)
' - '
" - "
\n - 換行
\ - 反斜桿
\t - tab鍵 縮進(制表符)
c.編碼字符: \u4位16進制數 - 將4位16進制數對應的編碼值轉換成字符
1)字符編碼
計算機只有直接存儲數字的能力, 不能直接存儲字符;
當需要計算機存儲字符的時候, 實質存的是字符對應的數字, 這個數字就是字符在計算機中的編碼;
每一個字符和數字的對應關系叫編碼表
2)ASCII碼表和Unicode編碼表
ASCII碼表是有美國國家標準制定的專門針對美國符號進行編碼的, 里面只包含一些特殊字符、字母和數字(不包含中文、日語、韓語等)
python采用的是Unicode編碼表: Unicode編碼表是對ASCII表的擴展, 包含了世界上所有國家所有語言的符號(萬國碼)
中文范圍: 0x4e00 ~ 0x9fa5
3)字符編碼相關的方法
chr(編碼值) - 將編碼轉換為對應的字符
ord(字符) - 獲取字符對應的編碼值
2.字符編碼
print(chr(65), chr(97))
print(chr(0x1800))
for x in range(0x4e00, 0x9fa5):
print(chr(x), end=' ') # 打印所有的漢字
str1 = '中文'
print(str1.encode().isalnum()) # 編碼過后isalnum()才能生效
二、字符串操作
1.獲取字符
str1 = 'hello world'
1)獲取單個字符
print(str1, str1[-1])
2)字符串切片
print(str1[2:6:2])
print(str1[2:4:-2]) # 切的空串
print(str1[3:])
print(str1[3::-1])
3)遍歷
for char in str1:
print(char, end=' ')
print(ord('a'), ord('z'))
# 練習: 統計一個字符串中小寫字母的個數
str2 = 'How Are You! Im Fine, THANK YOU!'
count = 0
for char in str2:
if ord(char) in range(97, 123):
count += 1
print(count)
2.字符串操作
1) + 和 *
字符串1 + 字符串2 -> 將字符串1和字符串2拼接在一起產生一個新的字符串
字符串 * n / n * 字符串 -> 字符串重復n次產生一個新的字符串
str1 = 'abc'
str2 = '123'
print(str1 + ':' + str2)
print(str1 * 3)
2) ==, !=
print('abc' == 'abc')
print('abc' != 'acb')
3)>, <, >=, <=
只能兩個字符串比較大小 - 從前往后找到一組不相等的字符, 比較他們編碼值的大小, 誰的編碼值大哪個字符串就大
print('abc' >= 'bc') # 從各自第一個字符依次相比編碼值
print('\u4e00', '\u9fa5') # 漢字范圍
4)in / not in
字符串1 in 字符串2 -> 判斷字符串2中是否包含字符串1(判斷字符串1是否是字符串2的字串)
str2 = 'How Are You! Im Fine, THANK YOU!'
print('how' in str2)
5) len(), max(), min(), sorted(), str()
字符串轉換: 所有的數據都可以轉換成字符串, 轉換的時候是將數據放在引號中
str2 = 'How Are You! Im Fine, THANK YOU!'
# 注意: 轉義字符和編碼字符的長度都是1
print(len(str2))
print(max(str2))
print(min(str2))
print(sorted(str2))
print(str(123))
6)r語法
在字符串的最前面加r或R, 可以阻止字符串中所有的轉義字符轉義
str2 = R'How Arer \n You! \\ Im Fine, THANK YOU!'
print(str2)
7)格式字符串
在字符串中用格式占位符表示字符串中不確定的部分
a.語法: 包含格式占位符的字符串 % (數據1, 數據2,...) - ()數據的個數和數據的類型要和前面格式占位符一一對應
b.格式占位符
%s - 字符串
%d - 整數
%.Nf - 浮點數,N控制小數點后小數的位數
%c - 字符(可以將數字轉換成字符)
注意: 所有的數據都可以使用%s來做格式占位符 2)所有的數據都可以使用%s來接受
三、字符串的相關方法
1.字符串的對齊方式
字符串.center(寬度, 填充字符=' ') - 居中
字符串.rjust(寬度, 填充字符=' ') - 居右
字符串.ljust(寬度, 填充字符=' ') - 居左
字符串.zfill(寬度) == 字符串.rjust(寬度, 填充字符='0') - 居右
str1 = 'abc'
print(str1.center(11, 'A')) # 居中
print(str1.ljust(9, 'A')) # 居左(默認)
print(str1.rjust(9, 'A')) # 居右
print(str1.zfill(9))
2.統計字串的個數
字符串1.count(字符串2) - 統計字符串1中字符串2出現的次數
str2 = 'How Are You! Im Fine, THANK YOU!'
print(str2.count('o'))
print(str2.count('You', 0, 8)) # 在下標[0, 8) 范圍內統計
3.獲取字串下標
print(str2.find('Are'))
print(str2.find('Are1'))
print(str2.index('Are'))
# print(str2.index('Are1')) # ValueError: substring not found
4.join方法
字符串1.join(序列) - 將序列中的元素用字符串1連接產生一個新的字符串
要求序列中的元素必須是字符串, 如果是字典key是字符串
print(';'.join(['小明1', '小明2', '小明3', '小明4']))
5.替換
字符串1.replace(字符串2, 字符串3, N) - 將字符串1中的前N字符串2都替換成字符串3(默認N=All)
str2 = 'How Are You! Im Fine, thank you!'
print(str2.replace('o', 'O'))
print(str2.replace('u', 'U', 1))
6.字符串切割
字符串1.split(字符串2) - 將字符串2作為切割點切割字符串1, 返回一個列表
str1 = 'How are you! Im Fine, thank you!'
print(str1.split(' ')) # ['How', 'Are', 'You!', 'Im', 'Fine,', 'thank', 'you!']
7.字符串的一些相關方法
print(str1.upper()) # HOW ARE YOU! IM FINE, THANK YOU! 轉為大寫
print(str1.lower()) # how are you! im fine, thank you! 轉為小寫
print(str1.capitalize()) # How are you! im fine, thank you!
print(str1.encode(encoding='UTF-8', errors='-1')) # b'How are you! Im Fine, thank you!'
print(str1.endswith('you!')) # True
str1 = 'How are you! \tIm Fine, thank you!'
print(str1.expandtabs(8)) # How are you! Im Fine, thank you!
print(str1.isalnum()) # False 判斷都是字母或數字
print(str1.isalpha()) # False 判斷都是字母
print(str1.isdigit()) # False 判斷都是數字
print(str1.islower()) # False 判斷都是小寫字母
print(str1.isupper()) # False 判斷都是大寫字母
print(str1.isdecimal()) # False 判斷都是數字字符
print(str1.isspace()) # False 判斷都是空白
str1 = 'How are you! Im Fine, thank you!'.title()
print(str1)
print(str1.istitle()) # True 判斷是否標題化
str1 = ' How are you! Im Fine, thank you! '
print(str1.strip()) # 截掉兩邊的空格
print(str1.lstrip()) # How are you! Im Fine, thank you! 截掉左邊的空格字符
print(str1.rstrip())
str1 = '444444444444How are you! Im Fine, thank you!4444444'
print(str1.strip('4')) # 截掉兩邊的自定字符
print(str1.lstrip('4')) # How are you! Im Fine, thank you!4444444
print(str1.rstrip('4')) # 444444444444How are you! Im Fine, thank you!
print(str1.rfind('How')) # 從右邊開始查找
str1 = 'How are you! Im Fine, thank you!'
print(str1.startswith('How')) # 判斷字符串是否以指定字符串開頭 True
print(str1.swapcase()) # 轉換大小寫
table1 = 'abc'
table2 = '123'
str1 = 'How are you! Im Fine, thank you!'
trans = str.maketrans(table1, table2) # 制作翻譯表
print(str1.translate(trans))