Python: 4 Ways to Declare a Multiline String (Block String)

Updated: May 25, 2023 By: Wolf Post a comment

This concise, straight-to-the-point article shows you a couple of different ways to declare a multiline string (block string) in Python.

Using Triple Quotes (Recommended)

You can use triple quotes (""" or ''') to declare multiline strings. This method allows you to include line breaks within the string without the need for explicit escape characters.

Example (triple double-quotes):

multiline_string = """This is a

Another example (triple single-quotes):

multiline_string = '''This is a

This approach is simple, intuitive, and widely used in modern Python projects.

If you want to include variables or expressions in a multiline string, you can use f-strings with triple quotes. With this, you can perform string interpolation within the multiline string.


name = "Wolf"
age = 99

multiline_string = f"""My name is {name}.
I am {age} years old."""

Using the Backslash Character

The slash / is an escape character that tells Python to ignore the following character and treat it as part of the string. You can use it to split a long string into multiple lines without creating a newline character.


multiline_string = "This is a \
multiline \

This technique works; it is not very readable or clear that you are creating a single string from multiple lines. Another important thing you should be aware of is that It does not preserve the indentation or alignment in the original code.

Using Parentheses for Implicit Line Continuation

Another way to declare a multiline string is by enclosing the string in parentheses. By doing so, you can write the string across multiple lines without the need for explicit line continuation characters.


multiline_string = (
    "Welcome "
    "to "
    "Sling Academy."

This approach works, but the line breaks or indentations in the original code won’t be maintained. When you print the string above, you will get this result:

Welcome to Sling Academy.

Using the join() Method

You can use the join() method to concatenate multiple strings in a list. By breaking the strings into separate lines and joining them with line breaks, you can create a multiline string.


multiline_string = '\n'.join([
    "This is a",

I think this technique is more verbose than necessary. I put it here for your reference, but using triple quotes is far better.