0

I have a python script that looks like this:

    print "Header 1"
    print "\t Sub-header 1"
    print "\t\t (*) Sentence 1 begins here and goes on... and ends here."

The sentences are being printed in a loop with the similar format, and it gives an output like this in the terminal:

Header 1
    Sub-header 1
        (*) Sentence 1 begins here and goes on...
and ends here.
        (*) Sentence 2 begins here and goes on ...
and ends here.
         .
         .
         .

Is there any way I can make the formatting as follows? :

Header 1
    Sub-header 1
        (*) Sentence 1 begins here and goes on...
            and ends here.
        (*) Sentence 2 begins here and goes on ...
            and ends here.
         .
         .
         .
4
  • 1
    I use something print print ('Text_1= %-20s | Text_2 = %0s | Text_3 = %6s\n') %('Here', 'Here_2', 'Here_3') to print on screen. Does this help you? (run it) Commented Sep 12, 2016 at 8:08
  • 2
    There is certainly a way to make the formatting as you wish, but the correct way to do this may depend of your existing code. Can you explain how this formatting is done for the moment? Commented Sep 12, 2016 at 8:26
  • @SnuKies: Thank you, but it doesn't help in my case. Due to the sentences being too long, the formatting still breaks in the terminal. Commented Sep 12, 2016 at 8:38
  • @Tryph: I've edited the question to explain how I've done the formatting. Any help is appreciated, thank you. Commented Sep 12, 2016 at 8:39

1 Answer 1

1

with the help of the textwrap module, it could be done quite easily:

import textwrap

LINE_LENGTH = 80
TAB_LENGTH = 8


def indent(text, indent="\t", level=0):
    return "{}{}".format(indent * level, text)

def indent_sentence(text, indent="\t", level=0, sentence_mark=" (*) "):
    indent_length = len(indent) if indent != "\t" else TAB_LENGTH
    wrapped = textwrap.wrap(text,
                            LINE_LENGTH
                            - indent_length * level
                            - len(sentence_mark))
    sentence_new_line = "\n{}{}".format(indent * level, " " * len(sentence_mark))
    return "{}{}{}".format(indent * level,
                           sentence_mark,
                           sentence_new_line.join(wrapped))


print indent("Header 1")
print indent("Sub-header 1", level=1)
print indent_sentence("Sentence 1 begins here and goes on... This is a very "
                      "long line that we will wrap because it is nicer to not "
                      "have to scroll horizontally. and ends here.",
                      level=2)

It prints this in a Windows console where tab are 8 chars long:

Header 1
        Sub-header 1
                 (*) Sentence 1 begins here and goes on... This is a very long
                     line that we will wrap because it is nicer to not have to
                     scroll horizontally. and ends here.
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! This was exactly what I was looking for!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.