Proper indentation for multiline strings in Python

avatar

Borislav Hadzhiev

Last updated: Aug 13, 2022

banner

Photo from Unsplash

Proper indentation for multiline strings in Python #

To properly indent multiline strings:

  1. Add a backslash at the end of the first line.
  2. Close the multiline string on the last line.
  3. Use the dedent() and indent() methods if you need to dedent or indent the multiline string.
main.py
from textwrap import dedent, indent from inspect import cleandoc multiline_str = """\ first second third""" # 👇️ with indentation # first # second # third print(multiline_str) # 👇️ without indentation # first # second # third print(dedent(multiline_str)) # 👇️ indent the multiline string a specific number of spaces # first # second # third print(indent(multiline_str, ' ')) # 👇️ using inspect.cleandoc # (removes empty lines at beginning and end, and leading whitespace) # first # second # third print(cleandoc(multiline_str))

The first thing to note when using multiline strings is to add a backslash at the end of the first line.

main.py
multiline_str = """\ first second third""" print(multiline_str)
If you don't add the backslash, you'll notice that an extra newline character gets added to the string.

Make sure to close the multiline string on the same line.

main.py
multiline_str = """\ first second third""" # 👈️ close on same line print(multiline_str)

If you don't close the multiline string on the same line, an extra newline character gets added at the end of the string.

You can use the textwrap.indent() and textwrap.dedent() methods to indent or dedent the multiline string.
main.py
from textwrap import dedent, indent multiline_str = """\ first second third""" # 👇️ without indentation # first # second # third print(dedent(multiline_str)) # 👇️ indent the multiline string a specific number of spaces # first # second # third print(indent(multiline_str, ' '))

The textwrap.dedent method takes a multiline string and removes the common leading whitespace from every line of the string.

The method is used to display multiline strings that are indented in the source code without any indentation.

The textwrap.indent method takes a multiline string and a prefix and adds the prefix to the beginning of each line of the string.

By default, the method adds the prefix to the beginning of each line that doesn't consist only of whitespace.

You can also use the inspect.cleandoc method if you want to remove the empty lines at the beginning and end of the multiline string and the leading whitespace.
main.py
from inspect import cleandoc multiline_str = """ first second third """ # # first # second # third # print(multiline_str) # first # second # third print(cleandoc(multiline_str))

I intentionally didn't add a backslash at the end of the first line and didn't close the multiline string on the same line.

Notice that the inspect.cleandoc method removed the empty lines at the beginning and end of the string and removed the leading whitespace.

If you don't want to remove the empty lines at the beginning and end of the multiline string, use the textwrap.dedent() method.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.