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

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

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Frequency-Power and Voltage-Reactive Power Characteristics of a Synchronous Generator — The…

Add Blazor Components to an Existing ASP.NET Core Web App

Mobile DevOps with Azure DevOps

Deep vs shallow copy

Generating/Signing Certificates with OpenSSL and Converting to Java Key Store(JKS)

Why it’s a good idea for beginner developers to start contributing to open source projects right…

Make a WhatsApp Bot For Free And Fun Via HTTP API!

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Ben Kehoe

Ben Kehoe

Cloud Robotics Research Scientist at @iRobot

More from Medium

A quick guide to using Loguru

Improve Python Code Quality Automatically Using Sourcery

A mac, coffee, text notebook

Poetry, A Better Version of Python Pipenv

Top 5 Python Blogs that you should follow and read in 2022