[Tutor] Splitting a string into n-sized bytes
Steve Nelson
sanelson at gmail.com
Wed Mar 15 07:38:57 CET 2006
On 3/14/06, Adam <adam.jtm30 at gmail.com> wrote:
> Hopefully that should point you in the right direction to do n-sized
> words as well.
Indeed - as I now have a function:
def nsplit(s, n):
return [s[i:i+n] for i in range(0, len(s), n)]
Incidentally I am currently going with:
def nsplit(s, n):
while s:
yield s[:n]
s = s[n:]
As my friend just showed me how generators work, and also that the
beginning and end of lists are implicit.
Very cool.
Thanks for all your help!
S.
More information about the Tutor
mailing list