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'
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论