Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print multiline string variable without indent [duplicate]

How do I print a multiline string variable to the console without the indent?

At the moment I tried this without success:

# Grid.
grid = """rnbqkbnr
          pppppppp
          ********
          ********
          ********
          PPPPPPPP
          RNBQKBNR"""

# Show grid.
print(grid)

The output is the following:

rnbqkbnr
             pppppppp
             ********
             ********
             ********
             PPPPPPPP
             RNBQKBNR

This is the output where I am looking for:

rnbqkbnr
pppppppp
********
********
********
PPPPPPPP
RNBQKBNR
like image 537
maartenpaauw Avatar asked Nov 30 '16 15:11

maartenpaauw


1 Answers

Use textwrap.dedent(text).

>>> from textwrap import dedent
>>> dedent("""\
    rnbqkbnr
    pppppppp
    ********
    ********
    ********
    PPPPPPPP
    RNBQKBNR""")
'rnbqkbnr\npppppppp\n********\n********\n********\nPPPPPPPP\nRNBQKBNR'
>>> print(_)
rnbqkbnr
pppppppp
********
********
********
PPPPPPPP
RNBQKBNR
like image 54
Peilonrayz Avatar answered Sep 18 '22 01:09

Peilonrayz