Split a string by length
Raymond Hettinger
python at rcn.com
Thu Mar 25 11:21:26 EST 2004
[Eyal Lotem]
> I am not sure this is the best method, but a very
> useful function I've been using is:
>
> def divide(seq, size):
> return [seq[i:i+size] for i in xrange(0,
> len(seq), size)]
>
> >>> divide('aabbcc', 2)
> ['aa', 'bb', 'cc']
>
> (Works on non-strings too)
>
> I put it in a common library and I reuse it all the
> time.
> I'd love to see it in the standard library, but until
> then.. :)
>
[Pascal]> > Hello,
> > How can I do to simulate this way:
> > 'aabbcc'.split(2) -> ['aa', 'bb', 'cc']
I am curious about what applications have a need for a function like
this. I've seen it in matrix work (like APL's ravel) but nowhere
else.
Raymond Hettinger
More information about the Python-list
mailing list