[Tutor] inserting new lines in long strings while printing

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

[Tutor] inserting new lines in long strings while printing

Abhishek Pratap
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.


Thanks!
-Abhi

_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: [Tutor] inserting new lines in long strings while printing

Mark Lawrence
On 07/03/2012 01:26, 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.
>
>
> Thanks!
> -Abhi
>
>
>
>
> _______________________________________________
> Tutor maillist  -  [hidden email]
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor

Simplest way I can think of (untested).

onebigstring = 'nbgkasgf;sh;slfgh;asdgh;adsdhg fg...'
for i in range(0, len(onebigstring), 100): # for Python3, xrange for
Python 2
     print onebigstring[i:i+100]

--
Cheers.

Mark Lawrence.

_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: [Tutor] inserting new lines in long strings while printing

Steven D'Aprano-8
In reply to this post by Abhishek Pratap
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


--
Steven
_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: [Tutor] inserting new lines in long strings while printing

Abhishek Pratap
thanks guys ..


-Abhi

On Tue, Mar 6, 2012 at 5:41 PM, Steven D'Aprano <[hidden email]> 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


--
Steven
_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: [Tutor] inserting new lines in long strings while printing

Peter Otten
In reply to this post by Abhishek Pratap
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


_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: [Tutor] inserting new lines in long strings while printing

Albert-jan Roskam
In reply to this post by Abhishek Pratap

From: Abhishek Pratap <[hidden email]>
To: Steven D'Aprano <[hidden email]>
Cc: [hidden email]
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 <[hidden email]> 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

_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor