Replace every n instances of a string

Terry Reedy tjreedy at
Sat Aug 16 01:05:36 CEST 2003

"Tom Cross" <thomasacross at> wrote in message
news:62de87da.0308151325.41ea4622 at
> I have a function that returns to me a text representation of
> data, which looks like this:
> I would like to add carriage returns...after each 12
> "\\u"s I encounter in the string.
> Any ideas?  Do I not want to search for "\\u" but instead just
> a \n after each 72 characters (equivalent to 12 \uXXXX codes)?
> this provide better performance?  If so, what would be the easiest
> to do that?

Split string into list of 6*n (72) char chunks and join with \n:

#unirep = textrep(unidata) #ie, call your func and store result. for
unirep =

blocklen = 6*4 #instead of 6*12 to get multiple lines with short
unilist = []
for i in range(0, len(unirep), blocklen):

unilines = '\n'.join(unilist)
>>> print unilines

Consider whether you want to change r'\n' to something else like
spaces for easier viewing.  If so, do so on unirep before chop into
blocks and adjust blocklen if replacement is not two chars.

Terry J. Reedy

Terry J. Reedy

More information about the Python-list mailing list