Python multi-line string literals with textwrap.dedent()

You’re writing some Python, and you need to write out a string containing newlines. You’ve got two options: a regular string with \n in it, or a multi-line string literal using three double quotes (are those sextuple quotes?), which looks like this:

my_string = """This
is a
multi-line string"""
assert my_string == "This\nis a\nmulti-line string"
def get_string():
return """This
is a
multi-line string"""
assert get_string() == "This\n is a\n multi-line string"
def get_string():
return textwrap.dedent("""This
is a
multi-line string""")
assert get_string() == "This\n is a\n multi-line string"
def get_string():
return """\
This
is a
multi-line string"""
assert get_string() == " This\n is a\n multi-line string"
def get_string():
return textwrap.dedent("""\
This
is a
multi-line string""")
assert get_string() == "This\nis a\nmulti-line string"

Cloud Robotics Research Scientist at @iRobot