Musings by @thedevel

Python Tip: Dedent Multi-line Strings

68 words · 25 Oct 2013

I've wanted to know this for a long time..

>>> import textwrap

>>> multiline = """
...     I am a few lines of text:
...     
...         with open('file.txt') as f:
...             f.write('a code block inside')
...     
...     demonstrating the use of `textwrap.dedent`.
... """

>>> print(textwrap.dedent(multiline))
I am a few lines of text:

    with open('file.txt') as f:
        f.write('a code block inside')

demonstrating the use of `textwrap.dedent`.