1.用途:記錄描述性質(zhì)的狀態(tài)草添,例如名字灸眼,性別蔫饰,國籍等
2. 定義方式:在引號(hào)('',"",''' ''',""" """)內(nèi)包含一串字符串
?s='hello'? #s=str('hello')
str功能可以把任意類型轉(zhuǎn)換成str類型
?res=str([1,2,3]) #"[1,2,3]"
?print(type(res))
3.常用操作加內(nèi)置方法
===========================》優(yōu)先掌握
1.按索引取值(正向取宏怔,反向染槲ā):只能取 不能改
s='hello word'
print(s[0],type(s[0]))? #'h'
print(s[-1])
非法操作
s[1]='E'? #不能修改? 會(huì)報(bào)錯(cuò)
s[222]
s[11]='A'
2.切片? (顧頭不顧尾烈菌,步長)===》屬于拷貝操作
s='hello word'
new_s=s[1:7]
print(new_s)
print(s)
new_s=s[1:7:2]? #1 3 5
print(new_s)
print(s)
new_s=s[:7:2]
new_s=[::2]? ? #0 2 4 6 8 10
print(new_s)? #h l o w r d
print(s)
new_s=s[::]? #完整拷貝阵幸,只留一個(gè)冒號(hào)就可以
print(new_s)
print(s)
s='hello word'
s1=s[1::-1]? # 不顯示取值? 會(huì)將所有的值都取完
print(s1)? ? ? ? # eh
print(len(s))
s2=s[9:7:-1]? #字符串長度為10 最后一個(gè)字符的索引為9
print(s2)? ? #d r
統(tǒng)計(jì)字符串的長度len
s='hello word'
print(len(s))
res=print('sfd')
print(res)#None
4.成員運(yùn)算 in 和not? in
s='hello word'
print('hel' in s )
print('egon' not in s )#語義明確花履,推薦使用
print(not 'egon' in s )
5.移除空白strip()? 碰到不屬于strip()里面的字符停下來,默認(rèn)()內(nèi)是空格,再從右邊開始
s='\n? ? ? ? ? hello? ? ? \t '
print(id(s))? ? ? #2171590232784
new_s=s.strip()
print(new_s)
print(id(s))? ? #2171590232784? ? ? ? 沒有改變?cè)址?/p>
去除左右兩邊非空白字符
s='hh#$%^&*hello word^&'.strip('h#o$%worfde^&ll*')
print(s)? ? 打印出來是空格
6.切分split:把字符串按照某個(gè)分隔符切成一個(gè)列表
userinfo='egonn123n18n3n1:'
res=userinfo.split('n')
print(res)
res='a'
res=res.split('a')
print(res)
純字符串組成的列表 (把列表按照某個(gè)格式切分成一個(gè)字符串)
p=['11','22','33','44','55']
res=':'.join(p)
print(res)
print('a a a a a'.split('a'))
res='my name is {x}{x}{y} my age is {y}{y}'.format(x=name,y=age)
print(res)
======================>需要掌握的操作
1.strip挚赊,lstrip,rstrip
print('*****hello****'.strip("*"))
print('*****hello****'.rstrip("*"))? #去除右邊的字符
print('*****hello****'.lstrip("*"))? #去除左邊的字符
2.lower诡壁,upper
msg='FGfFgjhjhFGfG'
res=msg.lower()? ? #全部變小寫
res1=msg.upper()? #全部變大寫
print(res,res1)
res=msg.swapcase()? #大小寫互相轉(zhuǎn)換
print(res)
3.startswith,endswith? 判斷一個(gè)字符是否為另一個(gè)字符串的首位或者末尾
msg='zhikun is Dsb'
print(msg.startswith('sb'))
print(msg.startswith('zhi'))
print(msg.endswith('sb'))
4.format的三種玩法
4.1 %s的方式
name='egon'
age=18
res1='my name is %s my age is %s'%(name,age)
print(res1)
4.2 formate的方式
name='egon'
age=18
res1='my name is {} my age is {}'.format(name,age)? ? ? ? 根據(jù)位置一一對(duì)應(yīng)
res2='my name is {0}{0}{0} my age is {0}{0}{0}{0}{0}{1}'.format(name,age)? 根據(jù)索引對(duì)應(yīng)
res3='my name is {x} my age is {y}'.format(x=name,y=age)? 根據(jù)變量名一一對(duì)應(yīng)
print(res1,res2,res3)
4.3 f''
name='egon'
age=18
res1=f'my name is {name} my age is {age}'? 在引號(hào)內(nèi)的花括號(hào)可以直接引用變量名來實(shí)現(xiàn)格式化輸出
print(res1)
了解:f搭配{}可以執(zhí)行字符串中的代碼
res=f'{len("hello")}'
print(res)
f'{len("hello")}'? 相當(dāng)于eval 都可以去運(yùn)行字符串中的代碼
res1=eval('len("hello")')
print(res1)
f包含的字符串可以放到多行
name='egon'
age=18
res1=f'my name is {name}' \
? ? ? f' my age is {age}'
print(res1)
在{}內(nèi)不能有\(zhòng)以及#
print(f'my name is {{egon}}')? #想要打印后加上{} 可以再外面再套一個(gè)花括號(hào)荠割,不能用轉(zhuǎn)譯符和#
print('勝率是%s%%' %70)? ? #想要打印百分比符號(hào) 需要在%后再加一個(gè)%取消前一個(gè)%的功能
5.split妹卿,rsplit
zhikun_dsb='zhikun:123:18'
print(zhikun_dsb.split(":"))? ? #以:為切割符 將字符串切割為字典 不指定切割次數(shù) 默認(rèn)從左到右全部切割
print(zhikun_dsb.split(":",1))? #以:為切割符,指定從左到右切割一次
print(zhikun_dsb.rsplit(":",1))? #以:為切割符蔑鹦,指定從右到左切割一次
6.join? 將純字符串的列表按照某種格式拼接為一個(gè)大字符串纽帖,
如果join內(nèi)為字符串,則會(huì)在字符串的每個(gè)子字符串后加入拼接的字符举反。
l=['zhukun_dsb','baobao_dsb','qiqi_dsb']
yangyang='='.join(l)
yangyang1='='.join('sdaweqr')
print(yangyang)
print(yangyang1)
?o=l[1]
y=l[2]
y=list(y)
print(y)
yangyang='>'.join('qiqi_dsb')#q>i>q>i>_>d>s>b
print(yangyang)
l=['zhukun_dsb']
?res='>'.join(l)
print(res)
name='egon'
age='18'
7.replace? 指定將字符串中的子字符替換成新的子字符懊直,產(chǎn)生了一個(gè)新的字符串。
msg = "***egon hello***"
res=msg.replace('*','').replace(' ','')
res=msg.strip('*').replace(' ','')
print(res)
s="lxx hahah wocale lxx sb 666"
res=s.replace('lxx','sb')
?res=s.replace('lxx','sb',1)
print(res)
print(s)
8.isdigit:判斷字符串是否由純數(shù)字組成
print("asdasdjsdkf54654".isdigit())? ? False
print('5454564546'.isdigit())? ? ? ? ? True
了解
1.find rfind index rindex count
1.1 find :從指定范圍內(nèi)查找子字符串的其實(shí)索引值火鼻,找到返回該數(shù)字的索引值室囊,找不到返回?cái)?shù)字-1
msg='tony say hello'
print(msg.find('m'))
print(msg.find('s'))
find還可以指定查找索引的范圍
print(msg.find('s',0,5))? #找不到返回?cái)?shù)字-1
print(msg.find('s',0,6))? #找不到返回?cái)?shù)字5
1.2 index : 與find一樣 功能都是查找子字符串在字符串中的索引值,但是找不到會(huì)報(bào)錯(cuò)魁索,所以一般推薦用find
print(msg.index('s'))
print(msg.index('m'))
也可以指定查找范圍
print(msg.index('s',0,6))
print(msg.index('s',0,5))
1.3 rfind rindex? 作用是從右到左查找子字符串在字符串中的位置融撞,輸出結(jié)果與find index 結(jié)果相同
print(msg.rfind('s'))? ? # 5
print(msg.rindex('s'))? ? #5
1.4 count 作用是統(tǒng)計(jì)子字符串在字符串中出現(xiàn)的次數(shù)
msg='tony say hello'
print(msg.count('x'))? #0
print(msg.count('l'))? #2
print(msg.count('l',0,-1))? #也可以指定查找范圍
2.center ljust rjust zfill
2.1? center? 作用是指定字符串的打印長度,并且字符串居中顯示粗蔚,不夠的可以用指定的字符填充
name='tony'
print(name.center(30,'*'))? #*************tony*************? ? ? 總長度為30個(gè)字符
2.2 ljust 作用是指定字符串的打印長度尝偎,并且字符串左對(duì)齊,不夠的用指定字符填充
print(name.ljust(20,'='))? #tony================ 總長度為20個(gè)字符
2.3 rjust 作用是指定字符串的打印長度鹏控,并且字符串右對(duì)齊致扯,不夠的用指定字符填充
print(name.rjust(10,'&'))? #&&&&&&tony? 總長度為10字符
2.4 zfill? 作用是指定字符串的打印長度,并且字符串右對(duì)齊当辐,不夠的用0填充
print(name.zfill(20))? # 0000000000000000tony? 總長度為20
3.expandtabs? 作用是修改制表符代表空格數(shù)? 疑問:制表符代表多少空格
name1='hello\tword'
print(name1)? ? #'hello? ? word'
print(name1.expandtabs(1))? #hello word
4.capatalize swapcase? title
4.1 captalize 首字母大寫
name='egoN read'
print(name.capitalize())? #Egon read
4.2 swapcase? 作用是將字符串的大小寫轉(zhuǎn)換過來
print(name.swapcase())? #EGOn READ
4.3 title? 每個(gè)單詞的首字母大寫
print(name.title())? #Egon Read
5.is 數(shù)字系列
在python3中
num1=b'4'
print(num1)? ? #bytes
num2=u'4'
print(num2)? #unicode,在python3中無需加u就是Unicode
num3='四' #中文數(shù)字
print(num3)
num4='Ⅷ' #羅馬數(shù)字
print(num4)
num9='III'
print(num4==num9)? ?False
5.1 isdigit 判斷字符串是否由純數(shù)字組成
print(num1.isdigit())
print(num2.isdigit())
print(num3.isdigit())? #False
print(num4.isdigit())? #False
5.2? isdecimal判斷字符串是否為Unicode類型bytes類型無isdecimal方法
print(num2.isdecimal())? #True
print(num3.isdecimal())? #False
print(num4.isdecimal())? #False
5.3 isnumberic 判斷字符串是否為unicode 中文數(shù)字?羅馬數(shù)字(百度教你打的羅馬字符是錯(cuò)誤的) (bytes類型無isnumberic方法)??
print(num2.isnumeric())? #True
print(num3.isnumeric())? #True
print(num4.isnumeric())? #False
三者都無法判斷浮點(diǎn)型
num5='4.3'
print(num5.isdigit())
print(num5.isdecimal())
print(num5.isnumeric())
全部為False
總結(jié):最常用的是isdigit抖僵,可以判斷bytes和Unicode類型,這也是最常見的數(shù)字應(yīng)用場(chǎng)景
如果要判斷中文數(shù)字或羅馬數(shù)字缘揪,則需要用到isnumeric耍群。
6 is其他
name='tony123'
print(name.isalnum())? ? #字符串中既可以包含數(shù)字也可以包含字母
# True
print(name.isalpha())? ? #字符串是否只包含字母
# False
print(name.isidentifier())? #? ? ? ????
# True
print(name.islower())? ? #字符串是否為純小寫
print(name.isupper())? ? #字符串是否為純大寫
print(name.isspace())? ? #字符串是否全為空格
print(name.istitle())? ? #字符串中的單詞首字母是否全為大寫