Indent On New Lines In Console

Question!

I have a large block of text. It has line breaks in it, but since the lines are still to long even with the line-breaks, it wraps to the next line. Since all of the other script functions have all lines indented one space, I would like this to match it. I understand that if I print just one line, I can just insert a space, and if I want to indent after a linebreak that fit in one line, I can just insert \n with a space after it.

How would I make every line in a block of text indent? e.g:

text = """This is a block of text. It keeps going on and on and on. It has some line breaks \n but mostly just keeps going on without breaks. The lines are sometimes too long, so they wrap to the next line, but they don't indent. I need to fix this"""

that would print as:

>>> print(text) 
  This is a block of text. It keeps going on
  and on and on. It has some line breaks

  but mostly just keeps going on without
  breaks. The lines are sometimes too long,
  so they wrap to the next line, but they 
  don't indent. I need to fix this


Answers

You said that join, split are failing to import. Try the following:

import re, textwrap

def myformatting(t):
    t=re.sub('\s+',' ',t); t=re.sub('^\s+','',t); t=re.sub('\s+$','',t)
    t=textwrap.wrap(t,width=40,initial_indent=' '*4,subsequent_indent=' '*8)
    s=""
    for i in (t): s=s+i+"\n"
    s=re.sub('\s+$','',s)
    return(s)

text = """\t\tThis is a block of text. It keeps going on
            and on and on. It has    some line breaks \n
            but mostly just keeps going on without
            breaks. The lines are sometimes too long,
            so they wrap to the next line, but they 
            don't indent. I need to fix this"""

text=myformatting(text)
print text
By : blackpen


Is this what you are looking for?

import textwrap
from string import join, split

text = """This is a block of text. It keeps going on
            and on and on. It has    some line breaks \n
            but mostly just keeps going on without
            breaks. The lines are sometimes too long,
            so they wrap to the next line, but they 
            don't indent. I need to fix this"""

print "\nPrinted as one line:\n"
t=join(text.split())
print t

print "\nPrinted as formatted paragraph:\n"
t=join(text.split())
t=textwrap.wrap(t,width=70,initial_indent=' '*4,subsequent_indent=' '*8)
t=join(t,"\n")
print t

Results:

Printed as one line:                                                                                                                                           

This is a block of text. It keeps going on and on and on. It has some line breaks but mostly just keeps going on without breaks. The lines are sometimes too lo
ng, so they wrap to the next line, but they don't indent. I need to fix this                                                                                   

Printed as formatted paragraph:                                                                                                                                

    This is a block of text. It keeps                                                                                                                          
        going on and on and on. It has                                                                                                                         
        some line breaks but mostly just                                                                                                                       
        keeps going on without breaks.                                                                                                                         
        The lines are sometimes too                                                                                                                            
        long, so they wrap to the next                                                                                                                         
        line, but they don't indent. I                                                                                                                         
        need to fix this                                                                                                                                       
By : blackpen


This video can help you solving your question :)
By: admin