<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <meta content="text/html; charset=ISO-8859-1"
      http-equiv="Content-Type">
    <title></title>
  </head>
  <body text="#000000" bgcolor="#ffffff">
    On 2:59 PM, Ian Kelly wrote:
    <blockquote
      cite="mid:%3CBANLkTi=XXDJjVzhEvq3y_20CzUYe6KapMA@mail.gmail.com%3E"
      type="cite">
      <pre wrap="">On Sat, Jun 4, 2011 at 12:09 PM, Chris Angelico <a class="moz-txt-link-rfc2396E" href="mailto:rosuav@gmail.com"><rosuav@gmail.com></a> wrote:
</pre>
      <blockquote type="cite">
        <pre wrap="">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.
</pre>
      </blockquote>
      <pre wrap="">
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, ''))
</pre>
    </blockquote>
    <br>
    As Ian discovered, the doc string for grouper() [on page
    <a class="moz-txt-link-freetext" href="http://docs.python.org/library/itertools.html">http://docs.python.org/library/itertools.html</a>] is wrong:<span
      class="s"><br>
    </span>
    <blockquote><span class="s">"grouper(3, 'ABCDEFG', 'x') --> ABC
        DEF Gxx"</span><br>
    </blockquote>
    grouper() doesn't return a string directly -- hence the need for
    "map('', join ..."<br>
    <br>
    Here's another implementation:<br>
    <blockquote><tt>def group(stg, count):</tt><br>
      <tt>    return [ stg[n:n+count] for n in range(len(stg)) if
        n%count==0 ]</tt><br>
      <br>
      <tt>print group('abcdefghij', 3)      # ['abc', 'def', 'ghi', 'j']</tt><br>
      <tt>print group('abcdefghijk' * 2, 7) # ['abcdefg', 'hijkabc',
        'defghij', 'k']</tt><br>
      <tt>print group('', 42)               # []</tt><br>
    </blockquote>
    <br>
    -John<br>
    <br>
    <br>
  </body>
</html>