Lambda question

Ian Kelly ian.g.kelly at gmail.com
Sat Jun 4 15:28:47 EDT 2011


On Sat, Jun 4, 2011 at 12:09 PM, Chris Angelico <rosuav at gmail.com> wrote:
> Python doesn't seem to have an inbuilt function to divide strings in
> this way. At least, I can't find it (except the special case where n
> is 1, which is simply 'list(string)'). Pike allows you to use the
> division operator: "Hello, world!"/3 is an array of 3-character
> strings. If there's anything in Python to do the same, I'm sure
> someone else will point it out.

Not strictly built-in, but using the "grouper" recipe from the
itertools docs, one could do this:

def strsection(x, n):
    return map(''.join, grouper(n, x, ''))

Cheers,
Ian



More information about the Python-list mailing list