Split a string by length
Peter Otten
__peter__ at web.de
Thu Mar 25 05:18:33 EST 2004
Pascal wrote:
> How can I do to simulate this way:
> 'aabbcc'.split(2) -> ['aa', 'bb', 'cc']
> I tried with a 'slice' but this didn't run:
> [item for item in 'aabbcc'[::2]] -> ['a', 'b', 'c']
>>> def chunks(s, chunklen):
... for i in range(0, len(s), chunklen):
... yield s[i:i+chunklen]
...
>>> list(chunks("aabbcc", 2))
['aa', 'bb', 'cc']
>>> list(chunks("aabbc", 2))
['aa', 'bb', 'c']
>>>
Peter
More information about the Python-list
mailing list