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、返回值為移除字符串頭尾指定的字符生成的新的字符串
三、replace:替換
replace() 方法把字符串中的 old(舊字符串) 替換成 new(新字符串),如果指定第三個參數max,則替換不超過 max 次。
返回字符串中的 old(舊字符串) 替換成 new(新字符串)后生成的新字符串,如果指定第三個參數max,則替換不超過 max 次。
四、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:可選。整數,規定結束搜索的位置。
十六、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——字符串常用方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Shell编程之case语句
- 下一篇: python的异常处理机制