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

发布时间 2023-09-28 22:13:58作者: 小鲨鱼2018

 

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

。