日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python——字符串常用方法

發布時間:2025/3/21 python 13 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python——字符串常用方法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、split:字符串分割

1、分割后為列表

str1='heloworld' print(str1.split('l')) ['he', 'owor', 'd']str1='hellloworld' print(str1.split('ll')) ['he', 'loworld']str1='aaabcd' print(str1.split('a')) ['', '', '', 'bcd'] print(str1.split('aa')) ['', 'abcd'] print(str1.split('aaa')) ['', 'bcd'] print(str1.split('aaaa')) ['aaabcd']

二、strip:去除字符串頭尾指定的字符(lstrip、rstrip)

1、用于移除字符串頭尾指定的字符(默認為空格或換行符)或字符序列
2、該方法只能刪除開頭或者是結尾的字符,不能刪除中間部分的字符。
3、返回值為移除字符串頭尾指定的字符生成的新的字符串

str2='helloworld' print(str2.strip('d')) helloworl print(str2.strip('world')) hestr2=" 000120abc021000 \n" print(str2.strip()) 000120abc021000刪除字符串開頭和結尾指定為0的字符 str2="000120abc021000" print(str2.strip('0')) 120abc021刪除字符串開頭指定為0的字符 print(str2.lstrip('0')) 120abc021000刪除字符串結尾指定為0的字符 print(str1.rstrip('0')) 000120abc021

三、replace:替換

replace() 方法把字符串中的 old(舊字符串) 替換成 new(新字符串),如果指定第三個參數max,則替換不超過 max 次。
返回字符串中的 old(舊字符串) 替換成 new(新字符串)后生成的新字符串,如果指定第三個參數max,則替換不超過 max 次。

str3='helloworld' print(str3.replace('l','中')) 替換所有的 he中中owor中d print(str3.replace('l','中',1)) 替換第一個 he中loworld

四、format:字符串的格式化

1、第一種方式 s='o' str4=f'hell{s}world' print(str4)2、第二種方式 s='o' str4='hell{}world'.format(s) print(str4)

五、index:獲取字符串中某個元素的索引,元素不存在,會拋出異常

str8='helloworld' print(str8.index('r')) #print(str8.index('i')) 會拋出異常:ValueError: substring not found

六、find:獲取字符串中某個元素的索引,元素不存在,返回-1

str7='helloworld' print(str7.find('r')) print(str7.find('i')) #元素不存在,返回-1

七、upper:將字符串中的字母變為大寫字母

str9='helloworld' print(str9.upper()) HELLOWORLD

八、lower:將字符串中的字母變為小寫字母

str10='Helloworld' print(str10.lower()) helloworld

九、count:統計字符串中指定元素的個數

str12='helloworld' print(str12.count('p')) 0

十、join:字符串的拼接

方法用于將序列中的元素以指定的字符連接生成一個新的字符串
返回通過指定字符連接序列中元素后生成的新字符串。

例1

str13='helloworld' lis=''.join(str13) print(lis) helloworld print(type(lis)) <class 'str'>

例2

lis=[1,2,3,4,5] lis1=','.join(lis) print(lis1) 拋出異常:TypeError: sequence item 0: expected str instance, int found 列表中的元素為只有為字符串類型的數據才能進行拼接

十一、startswith:檢查字符串是否以 “xxx” 開頭

檢查字符串是否以 “xxx” 開頭;如果字符串以指定的值開頭,則 startswith() 方法返回 True,否則返回 False。
語法:
string.startswith(value, start, end)
value:必需。檢查字符串是否以其開頭的值。
start:可選。整數,規定從哪個位置開始搜索。
end:可選。整數,規定結束搜索的位置。

案例1:檢查字符串是否以__開頭

str6='__helloworld' def func():if not str6.startswith('__'):return '1'else:return '2' res=func() print(res)

案例2:檢查字符串str6_2中從索引1到索引10中,是否已‘we’開頭

str6_1='Hello, welcome to my world' str6_2=str6_1.startswith('we',1,10) print(str6_2)

十二、encode:編碼

str14='helloworld' str14_1=str14.encode(encoding='utf8') print(str14_1) b'helloworld'

十三、decode:解碼

str15=str14_1.decode(encoding='utf8') print(str15) helloworld

十四、isdigit:檢測字符串是否只由數字組成。

如果字符串只包含數字則返回 True 否則返回 False。

def func(arg):if not arg.isdigit():return '不是數字'else:return '是數字'print(func('123123')) print(func('1zilv23')) print(func('zilv'))

十五、endswith:檢查字符串是否以 “xxx” 結尾

檢查字符串是否以 “xxx” 結尾;如果字符串以指定的值結尾,則 endswith() 方法返回 True,否則返回 False。
語法:
string.endswith(value, start, end)
value:必需。檢查字符串是否以其結尾的值。
start:可選。整數,規定從哪個位置開始搜索。
end:可選。整數,規定結束搜索的位置。

str6='__helloworld__' def func():if not str6.endswith('__'):return '1'else:return '2' res=func() print(res)

十六、len:計算字符串的長度

str16='helloworld' print(len(str16))

十七、S.isalnum() #是否全是字母和數字,并至少有一個字符

def func(arg):if arg.isalnum():return '全是字母和數字'else:return '不全是字母和數字'print(func('helloworld')) print(func('helloworld_'))

十八、S.isalpha() #是否全是字母,并至少有一個字符

def func(arg):if arg.isalpha():return '全是字母'else:return '不全是字母'print(func('helloworld')) print(func('helloworld_'))

十九、S.isspace() #是否全是空白字符,并至少有一個字符

def func(arg):if arg.isspace():return '全是空白字符'else:return '不全是空白字符'print(func('helloworld')) print(func(' '))

二十、S.islower() #S中的字母是否全是小寫

二十一、S.isupper() #S中的字母是否全是大寫

二十二、S.istitle() #S是否是首字母大寫的

《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀

總結

以上是生活随笔為你收集整理的python——字符串常用方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。