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

歡迎訪(fǎng)問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > python >内容正文

python

Python中字符串操作函数string.split('str1')和string.join(ls)

發(fā)布時(shí)間:2023/12/13 python 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python中字符串操作函数string.split('str1')和string.join(ls) 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

  Python中的字符串操作函數(shù)split 和 join能夠?qū)崿F(xiàn)字符串和列表之間的簡(jiǎn)單轉(zhuǎn)換,

  使用 .split()可以將字符串中特定部分以多個(gè)字符的形式,存儲(chǔ)成列表

1 def split(self, *args, **kwargs): # real signature unknown 2 """ 3 Return a list of the words in the string, using sep as the delimiter string. 4 5 sep 6 The delimiter according which to split the string. 7 None (the default value) means split according to any whitespace, 8 and discard empty strings from the result. 9 maxsplit 10 Maximum number of splits to do. 11 -1 (the default value) means no limit. 12 """ 13 pass

如上所說(shuō),split()函數(shù),使用字符串中的字符作為分隔符(sep),返回字符串分詞的列表(不含有作為分隔符的字符),如果分隔符為None,則以字符串中的空格作為分隔符;同時(shí)還可以傳入一個(gè)int參數(shù)

作為分隔的次數(shù),(默認(rèn)值 為 -1,不限制次數(shù))

eg:

>>>:s = 'Process finished with exit code 0'
>>>:s.split()# sep:None;maxsplit:-1.空格作為分隔符,分隔所有 ['Process', 'finished', 'with', 'exit', 'code', '0']>>>:s.split('i')#sep:‘i’;maxsplit:-1.‘i’作為分隔符,分割所有 ['Process f', 'n', 'shed w', 'th ex', 't code 0']>>>:s.split('i', 2)#sep:‘i’;maxsplit:2.‘i’作為分隔符,分割兩次 ['Process f', 'n', 'shed with exit code 0']

?

再進(jìn)一步:

>>>:s = 'Process finished with exiiit code 0'>>>:s.split('i') ['Process f', 'n', 'shed w', 'th ex', '', '', 't code 0']

  使用join()可以將列表中的字符串類(lèi)型數(shù)據(jù),組合成一個(gè)字符串:

1 def join(self, ab=None, pq=None, rs=None): # real signature unknown; restored from __doc__ 2 """ 3 Concatenate any number of strings. 4 5 The string whose method is called is inserted in between each given string. 6 The result is returned as a new string. 7 8 Example: '.'.join(['ab', 'pq', 'rs']) -> 'ab.pq.rs' 9 """ 10 pass

如上所說(shuō)連接任意數(shù)量的字符串,將字符串插入到被調(diào)用的兩兩字符串間,返回一個(gè)新的字符串。

?

總結(jié):

s = 'abcdabcd'ls = s.split('c')s2 = 'c'.join(ls)s = s2

上面利用兩個(gè)函數(shù)互相轉(zhuǎn)化,使用split()由字符串得到新列表,再使用join()由列表得到新字符串,

?

轉(zhuǎn)載于:https://www.cnblogs.com/wangwenhao072093/p/10580414.html

總結(jié)

以上是生活随笔為你收集整理的Python中字符串操作函数string.split('str1')和string.join(ls)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。