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表的擴展, 包含了世界上所有國家所有語言的符號(又叫萬國碼)
中文范圍: 0x4eoo ~ 0x9fa5
3)字符編碼相關方法
chr(編碼值) - 將編碼值轉換成字符
ord(字符) - 獲取字符對應的編碼值
# 1.字符串中的內容
str1 = 'abc\'12\"3'
# str1 = "ab"c'123"
print(str1)
str2 = 'abc\n123'
print(str2)
str3 = '\tabc\\n123'
str4 = ' abc\\n123'
print(str3, len(str3), len(str4))
str5 = "hh\u5e00abc"
print(str5)
str6 = 'abc 123'
str7 = 'abc123'
str8 = '1'
num = 1
# 2.字符編碼
print(chr(97), chr(65))
print(chr(0x1800))
for x in range(0x1800, 0x18af):
print(chr(x), end=' ')
print()
for x in range(0x1100, 0x11ff):
print(chr(x), end=' ')
print()
num = 0
for x in range(0x4e00, 0x9fa5):
num += 1
print(chr(x), end=' ')
if num % 35 == 0:
print()
print()
# ord()
print(ord('余'), ord('婷'))
print(hex(ord('余')), hex(ord('婷')))
name1 = '余婷'
name2 = '\u4f59\u5a77'
print(name1, name2)
print('z' > 'a') # True
print('Z' > 'a') # False
2.字符串操作
1.獲取字符 - 和列表獲取元素一樣
str1 = 'hello world!'
# 1)獲取單個字符
print(str1[0]) # 'h'
print(str1[-2]) # 'd'
# 2)字符串切片
print(str1[2:6:2]) # 'lo'
print(str1[2:6:-2]) # '' - 空串
print(str1[3:]) # 'lo world!'
print(str1[3::-1]) # 'lleh'
# 3)遍歷
for char in 'abc':
print(char)
##練習: 統計一個字符串中小寫字母的個數
str2 = 'How Are You! Im Fine, THANK YOU!'
count = 0
for char in str2:
if 97 <= ord(char) <= 122:
count += 1
print('小寫字母的個數:', count)
, <, >=, <=
只能兩個字符串比較大小 - 從前往后找到第一組不相等的字符,比較它們編碼值的大小,誰的編碼值大哪個字符串就大
'0' <= char <= '9' - 判斷是否是數字字符
'a' <= char <= 'z' - 判斷是否是小寫字母
'A' <= char <= 'Z' - 判斷是否是大寫字母
'a' <= char <= 'z' or 'A' <= char <= 'Z' - 判斷是否是字母
'\u4e00' <= char <= '\u9fa5' - 判斷是否是中文
print('abc' > 'bc') # False
print('abcf' > 'abca') # True
print('abcef' > 'aeaaaaaaa') # False
- in/ not in
字符串1 in 字符串2 -> 判斷字符串2中是否包含字符串1(判斷字符串1是否是字符串2的子串)
str3 = 'how are you!'
print('how' in str3) # True
print('h' in str3) # True
print('ha' in str3) # False
- len, max, min, sorted, str
字符串轉換: 所有的數據都可以轉換成字符串, 轉換的時候是將數據放在引號中
str3 = 'how are you!'
print(len(str3)) # 12
# 注意: 轉義字符串和編碼字符的長度都是1
str3 = '\\how are\tyou!'
print(len(str3)) # 13
str3 = '\u4effhow are\tyou!'
print(len(str3)) # 13
str3 = 'how are you!'
print(max(str3)) # y
print(sorted(str3)) # [' ', ' ', '!', 'a', 'e', 'h', 'o', 'o', 'r', 'u', 'w', 'y']
- r語法
在字符串的最前面加r或R,可以阻止字符串中所有的轉義字符轉義
str1 = '\thow\nare\'you!\u4e00'
print(str1, len(str1))
str1 = R'\thow\nare\'you!\u4e00'
print(str1, len(str1))
- 格式字符串
在字符串中用格式占位符表示字符串中不確定的部分
a.語法: 包含格式占位符的字符 % (數據1, 數據2,...) - ()中數據的個數和類型要和前面格式占位符一一對應
b.格式占位符
%s - 字符串
%d - 整數
%.Nf - 浮點數,N控制小數點后小數的位數
%c - 字符(可以將數字轉換成字符)
注意: 1)所有的數據都可以使用%s來做個數占位符 2)所有的數據都可以使用%s來接收
name = input('請輸入姓名:')
age = int(input('請輸入年齡:'))
gender = input('請輸入性別:')
xx今年xx歲,性別:X!
message = name+'今年'+str(age)+'歲,性別:' + gender + '!'
message2 = '%s今年%d歲,性別:%s!' % (name, age, gender)
print(message)
print(message2)
str4 = 'a: %s, b:%d, c:%f, d:%.2f e:%c' % ('HOW', 100, 1.23456, 1.23456, 'A')
print(str4)
str4 = 'a: %s, b:%d, c:%f, d:%.2f e:%c' % ('HOW', 100, 1.23456, 1.23456, 97)
print(str4)
str4 = 'a: %s, b:%s, c:%s, d:%s e:%s' % ('HOW', 100, 1.23456, 1.23456, 97)
print(str4)
3.字符串相關方法
1.對齊方式
字符串.center(寬度, 填充字符=' ') - 居中
字符串.ljust(寬度, 填充字符=' ') - 左對齊
字符串.rjust(寬度, 填充字符=' ')
字符串.zfill(寬度) == 字符串.rjust(寬度, 0)
str1 = 'abc'
print(str1.center(9, '+')) # +++abc+++, 居中
print(str1.ljust(9, '+')) # abc++++++, 左對齊
print(str1.rjust(9, '+')) # ++++++abc, 右對齊
print(str1.zfill(9)) # 000000abc
# 001, 002, 003, ..., 010, 100
num = 12 # 012
# num = 9 # 009
print(str(num).zfill(3))
2.統計子串的個數
字符串1.count(字符串2) - 統計字符串1中字符串2出現的次數
str1 = 'how are you! Im fine, thank you! and you?'
print(str1.count('you')) # 3
print(str1.count('h')) # 2
print(str1.count('you', 0, 12)) # 在下標是[0, 12)范圍內統計'you'的個數
3.獲取子串下標
print(str1.find('you')) # 8
print(str1.index('you')) # 8
print(str1.find('you1')) # -1
# print(str1.index('you1')) # ValueError: substring not found
4.join方法
字符串.join(序列) - 將序列中的元素用字符串連接產生一個新的字符串
要求序列中的元素必須是字符串, 如果是字典key是字符串
new_str1 = '+'.join('123')
print(new_str1) # '1+2+3'
new_str1 = 'and'.join(['小明', '小花', '小紅'])
print(new_str1) # 小明and小花and小紅
new_str1 = '-'.join({'name': '小明', 'age': 18,'gender': 'boy'})
print(new_str1) # name-age-gender
# new_str1 = '+'.join([1, 2, 3]) # TypeError: sequence item 0: expected str instance, int found
# print(new_str1)
5.替換
字符串1.replace(字符串2, 字符串3) - 將字符串1中所有的字符串2都替換成字符串3
字符串1.replace(字符串2, 字符串3, N) - 將字符串1中前N個字符串2替換成字符串3
str1 = 'how are you! Im fine, thank you! and you?'
new_str1 = str1.replace('you', 'YOU') # how are YOU! Im fine, thank YOU! and YOU?
print(new_str1)
new_str1 = str1.replace('you', 'me', 2) # how are me! Im fine, thank me! and you?
print(new_str1)
6.字符串切割
字符串1.split(字符串2) - 將字符串2作為切割點切割字符串1, 返回一個列表
str1 = 'how are you! Im fine, thank you! and you?'
str_list = str1.split(' ')
print(str_list) # ['how', 'are', 'you!', 'Im', 'fine,', 'thank', 'you!', 'and', 'you?']
str_list = str1.split('!')
print(str_list) # ['how are you', ' Im fine, thank you', ' and you?']