.join()
join將 容器物件 拆分並以指定的字元將列表內的元素(element)連線起來,返回字串(注:容器物件內的元素須為字元型別)
>>> a = ['no','pain','no','gain']
>>> '_ '.join(a)
'no_pain_no_gain'
>>>
注:容器物件內的元素須為字元型別
>>> b = ['I','am','no',1]
>>> '_'.join(b)
Traceback (most recent call last):
File "<pyshell#32>", line 1, in <module>
'_'.join(b)
TypeError: sequence item 3: expected string, int found
>>>
dict是以Key值作連線
>>> L = {'p':'P','y':'Y','t':'T','h':'H','o':'O','n':'N'}
>>> '_'.join(L)
'h_o_n_p_t_y' #dict 的無序性,使元素隨機連線。set 同理
>>>
.split()
與join相反,split以指定的字元將字串分割為單個元素(字元型別)並加入list中,返回一個List
>>> a = 'no_pian_no_gain'
>>> a.split('_')
['no', 'pian', 'no', 'gain']
>>>
split是可以設定切割多少個字元的
>>> a = 'no_pian_no_gain'
>>> a.split('_',2)
['no', 'pian', 'no_gain']
>>> a.split('_',1)
['no', 'pian_no_gain']
>>> a.split('_',0)
['no_pian_no_gain']
>>> a.split('_',-1)
['no', 'pian', 'no', 'gain']
>>>
可見split('_')與split('_',-1)返回的結果是一致的