python中实现按照固定位数拆分字符串

 

001、

[root@pc1 test2]# ls
test.py
[root@pc1 test2]# cat test.py       ## 测试程序
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import re

str1 = "abcdefghijklmn"
print(str1)

list1 = re.findall(".{3}", str1)    ## 按照每3位生成列表
print("-".join(list1))              ## 列表转换为字符串
[root@pc1 test2]# python3 test.py
abcdefghijklmn
abc-def-ghi-jkl

。 

 

posted @ 2023-09-28 22:01  小鲨鱼2018  阅读(37)  评论(0编辑  收藏  举报