[Tutor] inserting new lines in long strings while printing

Peter Otten __peter__ at web.de
Wed Mar 7 09:58:10 CET 2012


Abhishek Pratap wrote:

> I have this one big string in python which I want to print to a file
> inserting a new line after each 100 characters. Is there a slick way to do
> this without looping over the string.  I am pretty sure there shud be
> something its just I am new to the lang.

There is also textwrap.fill(). It honours word boundaries to some extent, so 
there may be lines that are shorter than the specified width.

>>> import textwrap
>>> print textwrap.fill("dere gewizzede bizz " + "a"*100, width=10)
dere
gewizzede
bizz aaaaa
aaaaaaaaaa
aaaaaaaaaa
aaaaaaaaaa
aaaaaaaaaa
aaaaaaaaaa
aaaaaaaaaa
aaaaaaaaaa
aaaaaaaaaa
aaaaaaaaaa
aaaaa




More information about the Tutor mailing list