1.什么是字符串(str)
- 1)字符串
- 字符串是容器型數(shù)據(jù)類型(序列);以單引號或者雙引號作為容器的標志(引號中所有的內(nèi)容都屬于字符串的元素)
- 'abc' > 元素分別是'a','b','c'三個元素
- 'a,b,c' > 元素分別是'a', ',', 'b', ',', 'c'五個元素
- 特點:不可變(不支持增刪改) , 有序(支持下標操作)
- 2)元素:
- 字符串中元素又叫字符,python中有字符的概念,但是沒有字符類型,長度是1的字符串就可以看成字符.
a.普通字符:
字母/數(shù)字/各國的文字和符號等(可以寫在引號中的符號)
b.轉(zhuǎn)義字符:
在字符串中在一些特定的符號前加\來表示特殊功能和意義
- :將\后面的內(nèi)容轉(zhuǎn)化為字符 \ > \ , ' > ' , " > "
- \n:換行操作
- \t:空格操作
- 注意:轉(zhuǎn)義字符的長度為1
c.編碼字符:
\u+4位的十六進制數(shù) - 將4位十六進制數(shù)對應(yīng)的編碼值轉(zhuǎn)換為字符
1)字符編碼
計算機只有直接存儲數(shù)字的能力,不能直接存儲字符:
當需要計算機存儲支付的時候,實質(zhì)存的是字符對應(yīng)的固定的數(shù)字,這個數(shù)字就是字符在計算機中的編碼
每一個字符和數(shù)字的對應(yīng)關(guān)系叫做編碼表
2)ASCII碼表和Unicode編碼表
- ASXII碼表是由美國國家標準制定的專門針對美國富豪進行編碼的,里面值包含一些特殊符號、字母和數(shù)字(不包含中文、日語等)
- ASCII碼表中大寫字母在小寫字母前,且中間不連續(xù)
- python采用的是Unicode編碼表:Unicode編碼表是對ASCII碼表的擴展,包含了世界上所有國家所有語言的符號(又叫萬國碼)
- 中文范圍:0x4e00 - 0x9fa5
3)字符編碼相關(guān)方法
- chr(編碼值) - 將編碼值轉(zhuǎn)換成字符
- ord(字符) - 獲取字符對應(yīng)的編碼值
"""
練習,如果字符串中想打印引號,可以在引號前加\
str1 = 'abc\'123' # abc'123
str2 = 'abc\t123' # abc 123
str3 = 'abc\\123' # abc\123
str4 = '\u4e00' # 一
2.字符串操作
1.獲取字符 - 和列表獲取元素一樣
1)獲取單個字符
2)字符串切片 - 和列表切片一樣
3)遍歷
練習:統(tǒng)計一個字符串中小寫字母的個數(shù)
str2 = 'asdfgASDFGHasd'
count = 0
for str in str2:
if 97 <= ord(str) <= 122:
count += 1
print(count)
2.字符串操作
1) + 和 *
- 字符串1 + 字符串2 -> 將字符串1和字符串2拼接在一起產(chǎn)生一個新的字符串
- 字符串 * N -> 字符串拼接N次產(chǎn)生一個新的字符串
str1 = 'abc'
str2 = '123'
print(str1 + str2) # abc123
print(str1 * 3) # abcabcabc
2)==, !=
print('abc' == 'acb') # False
print('abc' == 'abc') # True
3)> < >= <=
- 執(zhí)行兩個字符串比較大小(實際上從前往后找到第一組不相等的字符,比較字符的編碼值的大小)
- ‘0’ <= char <= '9' - 判斷是否是數(shù)字字符
- 'a' <= char <= 'z' - 判斷是否是小寫字母
- 'A' <= char <= 'Z' - 判斷是否是大寫字母
- '\u4e00' <= char <= '\u9fa5' - 判斷是否是漢字
print('abc' > 'bc') # False
print('abcf' > 'abcd') # True
print('何' > '哈') # True
4)in/not in
- 字符串1 in 字符串2 -> 判斷字符串2中是否包含字符串1(判斷字符串1是否是字符串2的子串)
print('ab' in 'abcdef') # True
print('abcde' in 'abc') # False
print('ab' in 'agseb') # False
5)len峭判, max宣蠕, min, sorted, str
- 注意:轉(zhuǎn)義字符和編碼字符的長度都是1
- max min求的是字符串中編碼最大/最小的字符
- sorted按照編碼大小排序,結(jié)果返回的是列表
- 字符串的轉(zhuǎn)換:所有的數(shù)據(jù)都可以轉(zhuǎn)換成字符串,轉(zhuǎn)換的時候是將數(shù)據(jù)放在引號中的
str3 = 'how are you!'
print(len(str3))
print(max(str3)) # y
print(sorted(str3)) # [' ', ' ', '!', 'a', 'e', 'h', 'o', 'o', 'r', 'u', 'w', 'y']
6).r語法
- 在字符串的最前面加r/R可以阻止字符串中所有的轉(zhuǎn)義字符轉(zhuǎn)義
str4 = r'ad\netrwe\'df\tdjfi'
print(str4) # ad\netrwe\'df\tdjfi
7)格式字符串
- 在字符串中用格式占位符表示字符串中不確定的部分
a.語法:
包含格式占位符的字符 % (數(shù)據(jù)1,數(shù)據(jù)2,...) - 后括號中數(shù)據(jù)的個數(shù)和類型要和前面格式占位符一一對應(yīng)
b.格式占位符
%s - 字符串
%d - 整數(shù)
%.Nf - 浮點數(shù)(N控制小數(shù)點后小數(shù)的位數(shù))
%c - 字符(如果是數(shù)字,會將數(shù)字當做編碼并轉(zhuǎn)義成字符)
注意:所有的數(shù)據(jù)都可以用%s作為占位符
補充:format
'{}{名字}{編號}'.format(名字 = '','')
name = input('請輸入姓名')
print(name + ',你好!', '%s,你好!'%name)
format的用法
print('{}是一個{}歲的{}'.format('小米','23','女孩子')) # 小米是一個23歲的女孩子
print('{0}是一個{2}歲的{1}'.format('小米','女孩子','23')) # 小米是一個23歲的女孩子
print('{name}是一個{age}歲的{gender}'.format(age='23',name='小米',gender='女孩子')) # 小米是一個23歲的女孩子
字符串的相關(guān)方法
1.對齊方式
- 居中:
字符串.center(寬度,填充字符=' ') - 左對齊:
字符串.ljust(寬度,填充字符=' ') - 右對齊:
字符串.rjust(寬度,填充字符=' ' ) - 左填充(用0填充到指定寬度):
字符串.zfill(寬度) == 字符串.rjust(寬度,0)
str1 = 'abc'
print(str1.center(10,'+')) # 居中:+++abc++++
print(str1.ljust(10,'+')) # 左對齊:abc+++++++
print(str1.rjust(10,'+')) # 左對齊:+++++++abc
print(str1.zfill(10)) # 左填充:0000000abc
2.統(tǒng)計子串的個數(shù)
- 字符串1.count(字符串2) - 統(tǒng)計字符串1中字符串2出現(xiàn)的次數(shù)
str1 = 'how are you! Im fin thank you and you '
print(str1.count('you')) # 3
print(str1.count('r')) # 1
print(str1.count('you',0,-5)) # 2 在指定位置的you出現(xiàn)的次數(shù)
3.查找指定子串并返回下標
- 字符串.find(子串) 或者 字符串.index(子串)
print(str1.find('you')) # 8 不存在不報錯
print(str1.index('you')) # 8 不存在會報錯
4.連接:join
- 字符串.join(序列) - 將序列中的元素用字符串連接產(chǎn)生一個新的字符串
- 要求序列中的元素必須是字符串,如果是字典,要求key全是字符串
new_str1 = '+'.join('123')
print(new_str1) # 1+2+3
5.替換
- 字符串1.replace(字符串2,字符串3) - 將字符串1中所有的字符串2都替換成字符串3
- 字符串1.replace(字符串2,字符串3,N) - 將字符串1中前N個字符串2都替換成字符串3
str1 = 'how are you! Im fin thank you and you '
new_str1 = str1.replace('you','YOU')
new_str2 = str1.replace('you','YOU',2)
print(new_str1) # how are YOU! Im fin thank YOU and YOU
print(new_str2) # how are YOU! Im fin thank YOU and you
6.字符串切割
- 字符串1.split(字符串2) - 將字符串2作為切割點切割字符串1
str1 = 'how are you! Im fine thank you and you '
print(str1.split(' ')) # ['how', 'are', 'you!', 'Im', 'fine', 'thank', 'you', 'and', 'you', '', '']
print(str1.split('!')) # ['how are you', ' Im fine thank you and you ']
print(str1.split()) # ['how', 'are', 'you!', 'Im', 'fine', 'thank', 'you', 'and', 'you']
字符串中其它方法
str1 = 'how are you! Im fine thank you and you'
# 將字符串第一個字符大寫
print(str1.capitalize())
# 字符串1.endwith(字符串2,開始位置撬碟,結(jié)束)
# 判斷在字符串1中從開始到結(jié)束位置,字符串是否以指定字符結(jié)尾
print(str1.endswith('you')) # True
print(str1.endswith('you',0,18)) # False
# 判斷在字符串1中從開始到結(jié)束位置凡橱,字符串是否以指定字符開始
print(str1.startswith('you'))
# 字符串.isalnum()
# 判斷一個不為空的字符串中是否所有字符都是字母或者數(shù)字
print(str1.isalnum()) #False
# 字符串.isalpha()
# 判斷一個不為空的字符串中是否所有字符都是字母或者數(shù)字
print(str1.isalpha()) #False
# 判斷一個不為空的字符串中是否只包含十進制數(shù)字
print(str1.isdecimal()) #False
# 判斷一個不為空的字符串中是否只包含數(shù)字
print(str1.isdigit()) #False
# 判斷一個不為空的字符串中是否全為小寫
print(str1.islower()) #False
# 判斷一個不為空的字符串中是否全為大寫
print(str1.isupper()) #False
# 將字符串中所有小寫字符轉(zhuǎn)化為大寫
print(str1.upper()) #HOW ARE YOU! IM FINE THANK YOU AND YOU
# 將字符串中所有大寫字符轉(zhuǎn)化為小寫
print(str1.lower()) #how are you! im fine thank you and you
# 翻轉(zhuǎn)字符串中的大小寫
print(str1.swapcase()) # HOW ARE YOU! iM FINE THANK YOU AND YOU
# 刪除字符串開始和末尾的空格小作,有r和l
print(str1.strip())