欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python字符串常用方法

 更新時間:2021年10月26日 10:22:48   作者:Silent丿丶黑羽  
這篇文章主要介紹了python字符串常用方法,find、count、replace、split、startswith、endswith等多種方法,需要的朋友可以參考一下文章得具體內(nèi)容,希望對你有所幫助

1、find(sub[, start[, end]])

在索引startend之間查找字符串sub
​找到,則返回最左端的索引值,未找到,則返回-1
startend都可省略,省略start說明從字符串開頭找
省略end說明查找到字符串結(jié)尾,全部省略則查找全部字符串

source_str = "There is a string accessing example"
print(source_str.find('r'))
>>> 3
 

2、count(sub, start, end)

返回字符串substartend之間出現(xiàn)的次數(shù)

source_str = "There is a string accessing example"
print(source_str.count('e'))
>>> 5
 

3、replace(old, new, count)

old代表需要替換的字符,new代表將要替代的字符,count代表替換的次數(shù)(省略則表示全部替換)

source_str = "There is a string accessing example"
print(source_str.replace('i', 'I', 1))
>>> There Is a string accessing example # 把小寫的i替換成了大寫的I
 

4、split(sep, maxsplit)

sep為分隔符切片,如果maxsplit有指定值,則僅分割maxsplit個字符串
分割后原來的str類型將轉(zhuǎn)換成list類型

source_str = "There is a string accessing example"
print(source_str.split(' ', 3))
>>> ['There', 'is', 'a', 'string accessing example'] # 這里指定maxsplit=3,代表只分割前3個
 

5、startswith(prefix, start, end)

判斷字符串是否是以prefix開頭,startend代表從哪個下標開始,哪個下標結(jié)束

source_str = "There is a string accessing example"
print(source_str.startswith('There', 0, 9))
>>> True
 

6、endswith(suffix, start, end)

判斷字符串是否以suffix結(jié)束,如果是返回True,否則返回False

source_str = "There is a string accessing example"
print(source_str.endswith('example'))
>>> True
 

7、lower

將所有大寫字符轉(zhuǎn)換成小寫

8、upper

將所有小寫字符轉(zhuǎn)換成大寫 

9、join

將列表拼接成字符串

list1 = ['ab', 'cd', 'ef']
print(" ".join(list1))
>>> ab cd ef
 

10、切片反轉(zhuǎn)

list2 = "hello"
print(list2[::-1])
>>> olleh

到此這篇關(guān)于python字符串常用方法的文章就介紹到這了,更多相關(guān)python字符串內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論