Python:将每n个单词拆分成更小的字符串

2024-06-28 11:35:09 发布

您现在位置:Python中文网/ 问答频道 /正文

情境:我有一大块文本,我想分解成更小的字符串。每n个字之后。在

text = This is a small example Text, showcasing my desired output.

应按n=4分为:

textList = ['This is a small', 'example Text, showcasing my', 'desired output.']

我的想法是把它分成一个只有单字的列表:

n = len(text)
textList = text.split(' ', n)

然后使用join()将其组合在一起,但我被卡住了,因为:

for x in range(0, 3):
    ' '.join(textList)

没有把我想要的输出列表放在一起


Tags: text文本列表outputisexamplemythis