1. 程式人生 > >python固定長度劃分字串

python固定長度劃分字串

import re

text = '123456789abcdefg'
textArr = re.findall('.{3}', text)
textArr.append(text[(len(textArr) * 3):])

print(textArr)
# ['123', '456', '789', 'abc', 'def', 'g']

import re


def cut_text(text, lenth):
    textArr = re.findall('.{' + str(lenth) + '}', text)
    textArr.append(text[(len(textArr)
* lenth):]) return textArr print(cut_text('123456789abcdefg', 3)) # ['123', '456', '789', 'abc', 'def', 'g']