python中拆分和拼接字符串

 

1、拆分字符串

>>> a = "2018:03:15"
>>> a
'2018:03:15'
>>> a.split(sep = ":")    ## sep指定拆分的分隔符
['2018', '03', '15']
>>> a = "ab:c:aaa:d:ee:fddf:dfd:34:2:89:887"
>>> a
'ab:c:aaa:d:ee:fddf:dfd:34:2:89:887'
>>> a.split(sep = ":")
['ab', 'c', 'aaa', 'd', 'ee', 'fddf', 'dfd', '34', '2', '89', '887']
>>> a.split(sep = ":",maxsplit = 1)   ## 设定分割次数
['ab', 'c:aaa:d:ee:fddf:dfd:34:2:89:887']
>>> a.split(sep = ":",maxsplit = 2)
['ab', 'c', 'aaa:d:ee:fddf:dfd:34:2:89:887']
>>> a.split(sep = ":",maxsplit = 3)
['ab', 'c', 'aaa', 'd:ee:fddf:dfd:34:2:89:887']

 

2、 拼接字符串

>>> a = ["aaa","bbb","ccc","ddd"]
>>> "_".join(a)   ## 指定连接符
'aaa_bbb_ccc_ddd'
>>> "===".join(a)
'aaa===bbb===ccc===ddd'
>>> b = ("aaa","bbb","ccc")
>>> type(b) 
<class 'tuple'>
>>> "-".join(b)    ## 拼接适用于一切可迭代对象
'aaa-bbb-ccc'

 

posted @ 2021-02-25 10:32  小鲨鱼2018  阅读(483)  评论(0编辑  收藏  举报