每第九个字符分开字符串?是否有可能每第n个字符拆分一条python字符串?例如,假设我有一个包含以下内容的字符串:'1234567890'我怎么能让它看起来像这样['12','34','56','78','90']
5 回答
![?](http://img1.sycdn.imooc.com/5458471300017f3702200220-100-100.jpg)
元芳怎么了
TA贡献1798条经验 获得超7个赞
>>> line = '1234567890'>>> n = 2>>> [line[i:i+n] for i in range(0, len(line), n)]['12', '34', '56', '78', '90']
![?](http://img1.sycdn.imooc.com/533e4c3300019caf02000200-100-100.jpg)
呼如林
TA贡献1798条经验 获得超3个赞
>>> import re>>> re.findall('..','1234567890')['12', '34', '56', '78', '90']
>>> import re>>> re.findall('..?', '123456789')['12', '34', '56', '78', '9']
>>> import re>>> re.findall('.{1,2}', '123456789')['12', '34', '56', '78', '9']
re.finditer
添加回答
举报
0/150
提交
取消