[Tutor] inserting new lines in long strings while printing

Albert-Jan Roskam fomcl at yahoo.com
Wed Mar 7 11:44:55 CET 2012




________________________________
From: Abhishek Pratap <abhishek.vit at gmail.com>
To: Steven D'Aprano <steve at pearwood.info> 
Cc: tutor at python.org 
Sent: Wednesday, March 7, 2012 2:56 AM
Subject: Re: [Tutor] inserting new lines in long strings while printing

thanks guys .. 
>
>
>
>
>-Abhi
>
>
>On Tue, Mar 6, 2012 at 5:41 PM, Steven D'Aprano <steve at pearwood.info> wrote:
>
>On Tue, Mar 06, 2012 at 05:26:26PM -0800, 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.
>>
>>>>> s = "a"*100
>>>>> print '\n'.join(s[i:i+10] for i in range(0, len(s), 10))
>>aaaaaaaaaa
>>aaaaaaaaaa
>>aaaaaaaaaa
>>aaaaaaaaaa
>>aaaaaaaaaa
>>aaaaaaaaaa
>>aaaaaaaaaa
>>aaaaaaaaaa
>>aaaaaaaaaa
>>aaaaaaaaaa
>>
>>
>----> Hi, using the textwrap module is also an option:
>>>> import textwrap
>>>> s = "a"*100
>>>> print "\n".join(textwrap.wrap(s, 10))
>aaaaaaaaaa
>aaaaaaaaaa
>aaaaaaaaaa
>aaaaaaaaaa
>aaaaaaaaaa
>aaaaaaaaaa
>aaaaaaaaaa
>aaaaaaaaaa
>aaaaaaaaaa
>aaaaaaaaaa
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20120307/a6524d11/attachment-0001.html>


More information about the Tutor mailing list