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'
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章