python中字符串內(nèi)置函數(shù)的用法總結(jié)
capitalize() 首字母大寫
a='someword' b=a.capitalize() print(b) —>Someword
casefold()&lower() 所有字母變小寫,casefold可將未知字符便小寫
a='someWORD' b=a.casefold() print(b) c=a.lower() print(c) —>someword —>someword
center(width,fillchar=None) 設(shè)置寬度,并將內(nèi)容居中,空白未知填充,一個(gè)字符
a='someword' b=a.center(30,'*') print(b)
count(sub,start=None,end=None) 去字符串中尋找,尋找子序列的出現(xiàn)次數(shù),可指定起止點(diǎn)
a='somewordsomeword' b=a.count(‘or') print(b) —>2
startswith(suffix,start=None,end=None)&endswith(suffix,start=None,end=None) 是否以XX開始/結(jié)束,可指定起止點(diǎn)
a='somewordsomeword' b=a.startswith(‘sa') c=a.endswith(‘ord') print(b) print(c) —>False —>True
find(sub,start=None,end=None) 尋找指定字符或字符串,并返回第一個(gè)位置,找不到返回-1,可指定起止點(diǎn)
a='somewordsomeword' b=a.find(‘me') print(b) —>2
format() 格式化,將一個(gè)字符串中的占位符替換為指定的值
test='I am {name},age {a}'
v=test.format(name='alex',a=19)
print(v)
—>i am alex,age 19
format_map() 格式化,傳入的值
test='iam{name},age{a}'
v=test.format_map({“name”:'alex',”a”:19})
print(v)
—>i am alex,age 19
isalnum() 字符串中是否只包含字母和數(shù)字
a='asdfs123*' b=a.isalnum() print(b) —>False
expandtabs(tabsize=number) 將字符串以number分割,并將tab補(bǔ)入
a='asdfs123\t523fgbdf' b=a.expandtabs(5) print(b) —>asdfs123 523fgbdf
isalpha() 字符串中是只包含字母
a='asdfsfgbdf' b=a.isalpha() print(b) —>True
isdecimal()&isdigit()&isnumeric() 字符串中是只包含數(shù)字,isdigit更為強(qiáng)大,isnumeric還可識(shí)別中文
a='132132②二' b=a.isdecimal() c=a.isdigit() d=a.isnumeric() print(b) print(c) print(d) —>False —>False —>True
isprintable() 是否存在不可顯示的字符如換行符
a='sdfgdfg\t' b=a.isprintable() print(b) —>False
isspace() 判斷是否全部為空格
a='dsvsdv' b=a.isspace() print(b) —>False
istitle()&title() 判斷是否為標(biāo)題,即首字母大寫&變?yōu)闃?biāo)題
a='follow uncased characters and lowercase characters only cased ones' b=a.istitle() print(b) c=a.title() print(c) —>False —>Follow Uncased Characters And Lowercase Characters Only Cased Ones
join(iterable) 將字符串中的每個(gè)元素按照指定分隔符進(jìn)行拼接
a='一二三四五六七' print(a) b='*' c=b.join(a) print(c) —>一二三四五六七 —>一二三四五六七
ljust(width,fillchar=None)&rjust(width,fillchar=None) 向右/左填充字符
a='hello' b=a.ljust(20,'*') c=a.rjust(20,'*') print(b) print(c) —>hello*************** —>***************hello
islower()&lower() 判斷是是否為全小寫&變?yōu)槿啃?/p>
a='Hello' b=a.islower() c=a.lower() print(b,c) —>False hello
isupper()&c=a.upper() 判斷是是否為全大寫&變?yōu)槿看髮?/p>
a='Hello' b=a.isupper() c=a.upper() print(b,c) —>False HELLO
lstrip(chars=None)&rstrip(chars=None)&strip(chars=None) 去除字符串左邊/右邊/兩邊的字符串,默認(rèn)空格,換行等
a='Hello' b=a.lstrip() c=a.rstrip() d=a.strip() print(b) print(c) print(d) —>Hello —> Hello —>Hello
maketrans(*args,**kwargs)&translate(table) 按maketrans對(duì)應(yīng)關(guān)系將translate中的字符串進(jìn)行替換
a='asdgfrfbcvzxrentas' b=str.maketrans(‘xdsa','1234') c=a.translate(b) print(c) —> 432gfrfbcvz1rent43
partition(sep)&rpartition(sep) 將字符串按指定字符分割成3段/或從右開始
a='helwloasvxcwaewc' b=a.partition(‘w') c=a.rpartition(‘w') print(b) print(c) —>(‘hel', ‘w', ‘loasvxcwaewc') —>(‘helwloasvxcwae', ‘w', ‘c')
split(sep=None,maxsplit=-1)&rsplit(sep=None,maxsplit=-1) 將字符串按指定字符串分割,分割后不保留
a='helwloasvxcwaewc' b=a.split(‘w',2) c=a.rsplit(‘w') print(b) print(c) —>[‘hel', ‘loasvxc', ‘a(chǎn)ewc'] —>[‘hel', ‘loasvxc', ‘a(chǎn)e', ‘c']
splitlines(keepends=None) 按照換行符進(jìn)行分割,帶true參數(shù)保留換行符
a='helwloas\nvxcwaewc\nafgasdfs' b=a.splitlines() c=a.splitlines(True) print(b) print(c) —>[‘helwloas', ‘vxcwaewc', ‘a(chǎn)fgasdfs'] —>[‘helwloas\n', ‘vxcwaewc\n', ‘a(chǎn)fgasdfs']
startswith(prefix,start=None,end=None)&endswith(prefix,start=None,end=None) 判斷字符串是否以指定字符開始/結(jié)束,可指定起止點(diǎn)
a='aefsfsfeeav' b=a.startswith(‘a(chǎn)e') c=a.endswith(‘a(chǎn)v',1,9) print(b) print(c) True —>False
swapcase() 小寫轉(zhuǎn)變?yōu)榇髮?/p>
a='aefsfsfeeav' b=a.swapcase() print(b) —>AEFSFSFEEAV
相關(guān)文章
python求兩個(gè)時(shí)間的時(shí)間差(實(shí)例代碼)
我們?cè)谟胮ython進(jìn)行分析的時(shí)候,可能會(huì)碰到計(jì)算兩個(gè)日期的時(shí)間差。下面為大家介紹一下如何計(jì)算兩個(gè)時(shí)間的時(shí)間差,需要的朋友可以參考下2022-11-11
opencv深入淺出了解機(jī)器學(xué)習(xí)和深度學(xué)習(xí)
機(jī)器學(xué)習(xí)是人工智能的核心,專門研究如何讓計(jì)算機(jī)模擬和學(xué)習(xí)人類的行為。?深度學(xué)習(xí)是機(jī)器學(xué)習(xí)中的一個(gè)熱門研究方向,它主要研究樣本數(shù)據(jù)的內(nèi)在規(guī)律和表示層次,讓計(jì)算機(jī)能夠讓人一樣具有分析與學(xué)習(xí)能力2022-03-03
Jupyter Notebook切換虛擬環(huán)境的三種方法
本文主要介紹了Jupyter Notebook切換虛擬環(huán)境的三種方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07
Python數(shù)據(jù)結(jié)構(gòu)與算法之完全樹與最小堆實(shí)例
這篇文章主要介紹了Python數(shù)據(jù)結(jié)構(gòu)與算法之完全樹與最小堆,結(jié)合實(shí)例形式分析了Python完全樹定義及堆排序功能實(shí)現(xiàn)相關(guān)操作技巧,需要的朋友可以參考下2017-12-12
python使用xlrd實(shí)現(xiàn)檢索excel中某列含有指定字符串記錄的方法
這篇文章主要介紹了python使用xlrd實(shí)現(xiàn)檢索excel中某列含有指定字符串記錄的方法,涉及Python使用xlrd模塊檢索Excel的技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-05-05
python smtplib模塊發(fā)送SSL/TLS安全郵件實(shí)例
這篇文章主要介紹了python smtplib模塊發(fā)送SSL/TLS安全郵件實(shí)例,本文講解了二種發(fā)送方式,需要的朋友可以參考下2015-04-04
python實(shí)現(xiàn)三次密碼驗(yàn)證的示例
這篇文章主要介紹了python實(shí)現(xiàn)三次密碼驗(yàn)證的示例,幫助大家更好的理解和學(xué)習(xí)使用python,感興趣的朋友可以了解下2021-04-04
使用jupyter notebook將文件保存為Markdown,HTML等文件格式
這篇文章主要介紹了使用jupyter notebook將文件保存為Markdown,HTML等文件格式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-04-04

