Splitting a string into groups of three characters

Cyril Bazin cyril.bazin at gmail.com
Mon Aug 8 16:04:44 EDT 2005


Another solution derived from an old discussion about the same problem?

def takeBy(s, n):
import itertools
list(''.join(x) for x in itertools.izip(*[iter(s)]*n))

(Hoping len(s) % n = 0)

Cyril

On 8 Aug 2005 11:04:31 -0700, lemon97 at gmail.com <lemon97 at gmail.com> wrote:
> 
> Yes i know i made a mistake,
> >['Hell','o W','orl','d']
> but you know what I mean lol,
> 
> I'll probly use
> John Machin's
> 
> def nsplit(s, n):
> return [s[k:k+n] for k in xrange(0, len(s), n)]
> 
> It seems fast, and does not require any imports.
> 
> But anyways, thank you for all your help, you rock! :)
> 
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20050808/69756f77/attachment.html>


More information about the Python-list mailing list