easy string formating question

Slawomir Nowaczyk slawomir.nowaczyk.847 at student.lu.se
Thu Aug 10 15:30:13 EDT 2006


On Thu, 10 Aug 2006 11:39:41 -0700
f pemberton <fpemberton133 at yahoo.com> wrote:

#> I have kind of an interesting string, it looks like a couple hundred
#> letters bunched together with no spaces. Anyway,  i'm trying to put a
#> "?" and a  (\n) newline after every 100th character of the string and
#> then write that string to a file. How would I go about doing that? Any
#> help would be much appreciated.

In addition to all the other ideas, you can try using StringIO

import StringIO
s = '1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
size = 10  # 100
input = StringIO.StringIO(s)
while input.tell()<input.len:     # is there really no better way to check for exhausted StringIO ?
    print input.read(size)+"?\n",
    # instead of print just write to a file or accumulate the result


-- 
 Best wishes,
   Slawomir Nowaczyk
     ( Slawomir.Nowaczyk at cs.lth.se )

"Reality is that which, when you stop believing in it,
doesn't go away." -- Philip K. Dick




More information about the Python-list mailing list