> 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.
>>> s = '1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' >>> size = 10 >>> print '?\n'.join([s[i:i+size] for i in xrange(0, len(s)+1, size)]) 1234567890? abcdefghij? klmnopqrst? uvwxyzABCD? EFGHIJKLMN? OPQRSTUVWX? YZ Just adjust "size" to 100 rather than 10. It may be a bit brute-force-ish, and there may be other more elegant ways that I don't know, but that list comprehension extracts pieces of "s" of size "size" and creates a list where each piece doesn't excede "size" characters. The join() then just smashes them all together, joined with your requested "quotation-mark followed by newline" And for the regexp-junkies in the crowd, you can use >>> import re >>> r = re.compile("(.{%i})" % size) >>> print r.sub(r"\1?\n", s) 1234567890? abcdefghij? klmnopqrst? uvwxyzABCD? EFGHIJKLMN? OPQRSTUVWX? YZ I'm sure there are plenty of other ways to do it. :) -tkc -- http://mail.python.org/mailman/listinfo/python-list