This snippet shows a fast and efficient way to cut strings in substrings with a fixed length (note that the last substring may be smaller).

import re
split5 = re.compile(r".{1,5}", re.DOTALL).findall

if __name__ == "__main__":
    print split5('"Give me bacon and eggs," said the other man.')
    # prints ['"Give', ' me b', 'acon ', 'and e', 'ggs,"', ' said', ' the ', 'other', ' man.']

An alternative is this

splitIt =  lambda d, s: [s[i:i+d] for i in range(0, len(s), d)]

if __name__ == "__main__":
    print splitIt(5, '"Give me bacon and eggs," said the other man.')

The following benchmark shows that, on my machine, split5() is about 22% faster than splitIt()

# benchmark for split5 and splitIt (tested with python 2.6)
from timeit import Timer
import re
split5 = re.compile(r".{1,5}", re.DOTALL).findall
splitIt =  lambda d, s: [s[i:i+d] for i in range(0, len(s), d)]

if __name__=='__main__':

    t = Timer("split5('Give me bacon and eggs, said the other man.')", "from __main__ import split5")
    print "split5: %.2f microsec"  % t.timeit()
    t = Timer("splitIt(5, 'Give me bacon and eggs, said the other man.')", "from __main__ import splitIt")
    print "splitIt: %.2f microsec"  % t.timeit()

""" my output --->
split5: 2.55 microsec
splitIt: 3.27 microsec
"""

however, you can gain 0.2 microsecs by replacing range with xrange in splitIt()

Edited 7 Years Ago by Gribouillis: n/a

The article starter has earned a lot of community kudos, and such articles offer a bounty for quality replies.