maartenpaauw maartenpaauw - 5 days ago 6
Python Question

Print multiline string variable without indent

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

Answer
print('\n'.join(grid.split()))

if you have inner spaces in your lines which you want to keep:

for line in grid.splitlines():
    print(line.strip())
Comments