[Tutor] String question.
Kent Johnson
kent37 at tds.net
Thu Apr 13 03:06:19 CEST 2006
Carlos Benevides wrote:
> All,
>
> I have a problem/question I'd like to pose to you guys on how best to
> accomplish the following. I have a string that will vary in size, what
> I would like to do is split into n size chunks. I am also not sure how
> best to represent the chunks. For example, say I have a len(s) = 200
> chars. I'd like to get one chunk with 160 of those chars and the
> remaining 40 in a second. Likewise, if I have a len(s) = 350, I'd like
> two chunks of 160 and a 3rd with the remainder.
>
> I don't know what it would best way to accomplish this. I have the idea
> of converting the string to a list of chars loop through it until size
> is met, create a new string with s=s[:160], then start again, and so
> on. Is this the only way to accomplish this? Or is there a more
> elegant/clever way?
Here is a good way and links to more ideas:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/425044
Kent
More information about the Tutor
mailing list