Python etc / textwrap.dedent

Published: 27 April 2021, 18:00

Multiline string literal preserves every symbol between opening and closing quotes, including indentation:

def f():
  return """
    hello
      world
  """
f()
# '\n    hello\n      world\n  '

A possible solution is to remove indentation, Python will still correctly parse the code:

def f():
  return """
hello
  world
"""
f()
# '\nhello\n  world\n'

However, it’s difficult to read because it looks like the literal is outside of the function body but it’s not. So, a much better solution is not to break the indentation but instead remove it from the string content using textwrap.dedent:

from textwrap import dedent

def f():
  return dedent("""
    hello
      world
  """)
f()
# '\nhello\n  world\n'