Python Equivalent for dd & fold

MRAB python at mrabarnett.plus.com
Thu Jul 16 13:07:54 EDT 2009


Michiel Overtoom wrote:
> seldan24 wrote:
> 
>> I know that Emile suggested that I can slice out the substrings rather
>> than do the gradual trimming of the string variable as is being done
>> by moving around the length. 
> 
> An excellent idea.
> 
> def fold(s,chunklength):
>     offset=0
>     while offset<len(s):
>         print s[offset:offset+chunklength]
>         offset+=chunklength
> 
More Pythonic:

     for offset in range(0, len(s), chunklength):
         print s[offset : offset + chunklength]

> s="A very long string indeed. Really that long? Indeed."
> fold(s,10)
> 




More information about the Python-list mailing list