#一:find 尋找
mystr = 'hello world itcast and itcastcpp'
# mystr.find(str, start=0, end=len(mystr))? 括號內(nèi)內(nèi)容:尋找的字符串,下標起始點交排,下標中止點? ? 從左邊開始
mystr.find('itcast')? ? ? ? #結(jié)果顯示為12民珍,說明當前的itcast在下標為12處開始顯示
mystr.find('itcast',12,18)? #可以找出當前itcast所在的位置
mystr.find('itcast',12,17)? #找不到所在位置就會顯示出-1
#二:index? 索引( 和find效果一樣而昨,區(qū)別在于找不出結(jié)果會報錯 )??
mystr = 'hello world itcast and itcastcpp'
# mystr.index(str, start=0, end=len(mystr))? ? 括號內(nèi)內(nèi)容:尋找的字符串,下標起始點诫给,下標中止點
mystr.index('itcast')? ? ? #結(jié)果顯示為12居灯,說明當前的itcast在下標為12處開始顯示
mystr.index('itcast',12,18) #可以找出當前itcast所在的位置
mystr.index('itcast',12,18) #找不到所在位置,就會報錯
#三:count? ? 數(shù)數(shù),計算總數(shù)
mystr = 'hello world itcast and itcastcpp'
# mystr.count(str, start=0, end=len(mystr))? ? 括號內(nèi)內(nèi)容:所需字符串镀迂,下標起始點丁溅,下標中止點
mystr.count('itcast')? ? #結(jié)果為2? 返回str在start和end之間在mystr里面出現(xiàn)的次數(shù)
#四:replace? 替換
name ='hello world ha ha'
#name.replace(str1, str2, name.count(str1))? 括號的內(nèi)容為:變量中的str1,需要轉(zhuǎn)換的成的str2,需要替換的次數(shù)
name.replace('ha','Ha')? ? # 結(jié)果為'hello world Ha Ha'? ? 把name中的str1替換成str2,如果count制定探遵,則替換不超過count的次數(shù)
name.replace('ha','Ha',1)? #結(jié)果為'hello world Ha ha'
#五:split? ? 分隔窟赏,分開
name='hello world \t ha ha ha \nha'
# name.split(str=' ',2)? 括號內(nèi)的內(nèi)容為想要分隔的字符串切片? 從頭開始切片(字符串),數(shù)字為幾就是切幾個出來
name.split()? ? ? ? #結(jié)果為:'hello', 'world', 'ha', 'ha', 'ha', 'ha'
name.split(' ')? ? #結(jié)果為:'hello'箱季,'world','\t', 'ha', 'ha', 'ha', '\nha'
name.split(' ',2)? #結(jié)果為:'hello', 'world', '\t ha ha ha \nha '
name.split(' ',3)? #結(jié)果為:'hello', 'world', '\t', 'ha ha ha \nha'? 以str為分隔符切片name涯穷,如果maxspilt有制定之,則僅分隔maxspilt個子字符串
#六:capitalize? 用大寫字母(第一個字符串)打印
name='hello world ha ha ha ha'
# name.capitalize()? ? 括號內(nèi)無任何內(nèi)容
name.capitalize()? #結(jié)果為:'Hello world ha ha ha ha '? 把字符串的第一個字符大寫
#七:title? 加標題==將每個字符大寫
name='hello world ha ha ha ha'
#name.title()? ? 括號內(nèi)無任何內(nèi)容
name.title()? ? #結(jié)果為:'Hello World Ha Ha Ha Ha '? ? 把字符串的每個單詞首字母大寫
#八:startswith? ? 開始于藏雏,以什么開始? ? 值為:True? /? False
name='Are you sure'
#name.startswith('')? ? 括號內(nèi)的內(nèi)容可以是字符串的首字母或者字符.
name.startswith('Are')? #結(jié)果為:True
name.startswith('A')? ? #結(jié)果為:True
name.startswith('are')? #結(jié)果為:False? ? 檢查字符串是否以'A'或者'Are'開頭
#九:endswitch? ? 結(jié)尾于拷况,以什么為結(jié)尾? 值為:True? /? False? ? 用法和#startswith相同
#十:lower? 下降? ? 將所有的字體的大寫轉(zhuǎn)換為小寫
name ='HELLO World ha ha'
# name.lower()? 括號內(nèi)無內(nèi)容
name.lower()? #結(jié)果為:'hello world ha ha'? ? 轉(zhuǎn)換name中所有的大寫字符為小寫
#十一:upper? 上升? ? 將所有字體都轉(zhuǎn)換成為大寫的
name='hello world ha ha ha ha'
# name.upper()? ? 括號內(nèi)無任何內(nèi)容
name.upper()? ? #結(jié)果為:'HELLO WORLD HA HA HA HA '? 轉(zhuǎn)換name中的小寫字母為大寫
# 十二:ljust? ? 左邊對齊,系統(tǒng)自動補齊
name = 'python'
# name.ljust(width)? 括號內(nèi)的內(nèi)容為需要擴充至多少長度,系統(tǒng)會自動補齊這個長度
name.ljust(10)? #結(jié)果為:'python? ? '? 右邊自動填充4個空格使得長度為10? 即左對齊? 返回一個原字符串左對齊赚瘦,并使用空格填充之長度width的新字符串
#十三:rjust? ? 右邊對齊粟誓,系統(tǒng)自動補齊
name = 'python'
# name.rjust(width/width=n)? 括號內(nèi)的內(nèi)容為width=n或者直接數(shù)字,系統(tǒng)自動補齊這個長度
name.rjust(10)? #結(jié)果為:'? ? python'? ? 左邊自動填充4個空格使得長度為10? 即右對齊? 返回一個原字符串右對齊起意,并使用空格填充至長度width的新字符串
#十四:center? ? 中心鹰服,中央? ? 將字符串居中顯示
name = 'python'
# name.center(width=)? 括號中的內(nèi)容為想讓字符串成為多長的新字符,從而進行居中揽咕。
name.center(20)? #結(jié)果為:'? ? ? python? ? ? '? 一共20個字符串悲酷,將python居中。? ? 返回一個原字符串居中亲善,并使用空格填充至長度width的新字符串
#十五:lstrip? 左奪去? 刪除左邊的空白符
name = '? ? ? ? python'
# name.lstrip()? 括號內(nèi)沒有任何內(nèi)容
name.lstrip()? #結(jié)果為:'python'? 刪除name左邊的空白字符
#十六:rstrip? 右奪去? ? 刪除右邊的空白字符
name ='python? ? ? ? ? '
# name.rstrip()? ? 括號內(nèi)無任何內(nèi)容
name.rstrip()? ? #結(jié)果為:'python'? 刪除name右邊的空白字符
#十七:strip? 奪去? ? 刪除兩端的空白字符
name = ' \t\n? python? \t\n? '
# name.strip()? ? 括號內(nèi)沒有任何內(nèi)容
name.strip()? #結(jié)果為:'python'? 刪除name字符串兩端的空白字符
#十八:rfind? 反向查找? ? 從右邊開始查找
name = 'hello world itcast and itcastcpp'
# name.rfind(str,start=0,end=len(name))? ? 括號內(nèi)的內(nèi)容為:所要查找的字符串? 下標起始點? 下標中止點? 從右邊開始
name.rfind('itcast',12,32)? #結(jié)果為23? 從12到32下標中设易,從右邊開始查找,所以查找的右邊的23處
name.rfind('itcast',24,32)? #結(jié)果為-1? 從24到32下標中逗爹,從右邊開始查找,并沒有擦回到嚎于,輸出-1
#十九:rindex? 反向索引
name = 'hello world itcast and itcastcpp'
# name.rindex(str,start=0,end=len(name))? 括號內(nèi)的內(nèi)容為:所要查找的字符串? 下標起始點? 下標中止點? 從右邊開始
name.rindex('Itcast')? ? ? ? #結(jié)果為:會報錯
name.rfind('itcast',12,32)? #結(jié)果為:23
name.rfind('itcast',24,32)? #結(jié)果為:會報錯
#二十:partition? ? 分隔掘而,分離? 將字符串以目標字符串分為前中后? 從左邊開始
name = 'hello world itcast and itcastcpp'
# name.partition('str')? ? ? 括號內(nèi)的內(nèi)容為想要分隔開來的字符串
name.partition('world')? #結(jié)果為:'hello' ,'world', 'itcast and itcastcpp'? 將name以world分割成三部分,world前于购,world,world后
#二十一:rpartition? 反向分隔? 將字符串以目標字符串氛圍前中后? ? 從右邊開始
name = 'hello world itcast and itcastcpp'
# name.rpartition('str')? ? 括號內(nèi)的內(nèi)容為想要分割開來的字符串
name.partition('itcast')? #結(jié)果為:'hello world','itcast','and itcastcpp'? ? 將name以itcast分割成三部分袍睡,從左邊開始,itcast前肋僧,itcast斑胜,itcast后
name.rpartition('itcast')? #結(jié)果為:'hello world itcast and','itcast','cpp'? 將name以itcast分割成三部分,從右邊開始嫌吠,itcast前止潘,itcast,itcast后
#二十二:splitlines? 行分隔? 將換行的字符串返回為一行
name='hello\npython'
# name.splitlines()? ? 括號內(nèi)無任何內(nèi)容
name.splitlines()? ? #結(jié)果為:'hello','python'? 按照行分隔辫诅,返回一個包含各行作為元素的列表
#二十三:isalpha? 是否是字母? 結(jié)果為:True? /? False
name='hello python'
# name.isalpha()? ? 括號內(nèi)無任何內(nèi)容
name.isalpha()? #結(jié)果為:? False? 沒有空格時為True凭戴。? 如果name所有的字符都是字母,則返回True,否則返回False
#二十四:isdigit? 是否為數(shù)字? ? 結(jié)果為:True? /? False
name = '123456'
# name.isdigit()? ? 括號內(nèi)無任何內(nèi)容
name.isdigit()? ? #結(jié)果為:True? 僅有全部字符串為數(shù)字時才為True? 如果name只包含數(shù)字則返回True否則返回False
#二十五:isalnum? 是否為字母或者數(shù)字? 結(jié)果為:True? /? False
name = 'hello123'
#name.isalnum? ? 括號內(nèi)無任何內(nèi)容
name.isalnum()? ? #結(jié)果為:True
name = 'hello 123'
name.isalnum()? ? #結(jié)果為:False? ? 如果name所有字符都是字母或數(shù)字則返回True炕矮,否則返回False
#二十六:isspace? 是否只含有空格? ? ? 結(jié)果為:True? /? False
name = '? '
# name.isspace()? 括號內(nèi)無任何內(nèi)容
name.isspace()? ? #結(jié)果為:True
name = ''
name.isspace()? ? #結(jié)果為:False? ? ? 如果name中只包含空格么夫,則返回True,否則返回False.
#二十七:join? ? 加入
str = ' '
li = ['my','name','is','dongGe']
str.join(li)
'my name is dongGe'
str = '_'
li = ['my','name','is','dongGe']
str.join(li)
'my_name_is_dongGe'