python-列表與字符串之間的操作

split():將字符串按某字符分割成列表,默認空格爲分割字符

>>> s = 'this is a test'
>>> t = s.split()
>>> t
['this', 'is', 'a', 'test']
>>> s = "13:50"
>>> t = s.split(':')
>>> t
['13', '50']
>>> s = "13::50"
>>> t = s.split('::')
>>> t
['13', '50']
>>> s = "13::50"
>>> t = s.split(':')
>>> t
['13', '', '50']

join():將列表用某符號連接成字符串

>>> s = 'this is a test'
>>> t = s.split()
>>> s
'this is a test'
>>> t
['this', 'is', 'a', 'test']
>>> s1 = '+'.join(t)
>>> s1
'this+is+a+test'
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章