74
\$\begingroup\$

The following function works well to print out a help message for the program I have written. It prints a multi-line string message that describes the command line usage with some examples:

def usage():

    print(
        """
Usage examples:

Test deployment:
       $ fab [noinput] test deploy

Staging deployment:
       $ fab [noinput] staging deploy

Production deployment:
       $ fab [noinput] production deploy
""")

Unfortunately, in order to make the message look good in the output, the code block currently looks ugly with the string message making the indentation of the code look wrong. Is there a way to improve the code so that it is more readable, similar to the following code (which does not work - it is just an example) where the indentation is more readable?

def usage():

    print(
        """
        Usage examples:
        Test deployment:
            $ fab [noinput] test deploy
        Staging deployment:
          $ fab [noinput] staging deploy
        Production deployment:
          $ fab [noinput] production deploy
        """
    )

How can the readability be improved while still producing results that look good in the output?

\$\endgroup\$

closed as off-topic by Peilonrayz, yuri, Mast, IEatBagels, Zeta Aug 19 '18 at 16:20

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – Peilonrayz, yuri, Mast, IEatBagels, Zeta
If this question can be reworded to fit the rules in the help center, please edit the question.

79
\$\begingroup\$

You can use textwrap.dedent to remove common whitespace from the start of all lines in a multiline string:

>>> import textwrap
>>> print(textwrap.dedent(
        """
        Usage examples:
        Test deployment:
            $ fab [noinput] test deploy
        Staging deployment:
            $ fab [noinput] staging deploy
        Production deployment:
            $ fab [noinput] production deploy
        """
))

Usage examples:
Test deployment:
    $ fab [noinput] test deploy
Staging deployment:
    $ fab [noinput] staging deploy
Production deployment:
    $ fab [noinput] production deploy

As it says in the docs:

This can be used to make triple-quoted strings line up with the left edge of the display, while still presenting them in the source code in indented form.

\$\endgroup\$
23
\$\begingroup\$

For smaller pieces of text, I tend to just close the double quotes and open them on the next line. Then you'll have to add the newlines yourself using \n though:

print(
    "Usage examples:\n"
    "Test deployment:\n"
    "    $ fab [noinput] test deploy\n"
    "Staging deployment:\n"
    "    $ fab [noinput] staging deploy\n"
    "Production deployment:\n"
    "    $ fab [noinput] production deploy\n"
)
\$\endgroup\$
  • 6
    \$\begingroup\$ If you don't like adding '\n' in each string, you can also do print( "Usage examples:", "Test deployment:", " $ fab [noinput] test deploy", …, sep='\n') \$\endgroup\$ – kundor Jan 23 '17 at 16:29
17
\$\begingroup\$

When I need some usage-text or help-text about a program or module, there is another option, at the begin of the source code:

#!/bash/python  
"""  
Usage examples, Test deployment,  
and any other text you like  
"""

And when I need to print it somewhere in the program:

print(`__doc__`)
\$\endgroup\$
7
\$\begingroup\$

Also you may want to use .join() method:

def usage():

    print('\n'.join([        
        'Usage examples:',
        'Test deployment:',
        '    $ fab [noinput] test deploy',
        'Staging deployment:',
        '    $ fab [noinput] staging deploy',
        'Production deployment:',
        '    $ fab [noinput] production deploy'        
        ]))
\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.