且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在python中将很长的字符串拆分为较短的字符串列表

更新时间:2023-09-06 13:55:28

>>> s = "This is a very long string with many many many many and many more sentences and there is not one character that i can use to split by, just by number of words"
>>> l = s.split()
>>> n = 5
>>> [' '.join(l[x:x+n]) for x in xrange(0, len(l), n)]
['This is a very long',
 'string with many many many',
 'many and many more sentences',
 'and there is not one',
 'character that i can use',
 'to split by, just by',
 'number of words']