How To Write Multiline Format Strings In Python

Pavol Kutaj
1 min readJan 22, 2022

--

The aim of this is to outline 4 simple steps for writing multiline formatted strings in Python.

  1. place f-strings in parentheses for implied line continuation (or directly within a print statement)
  2. do not separate them by comma and do not use backslash for line continuation
  3. do signal a newline with \n except on the last line
  4. each f string line has its own f"string" notation
The preferred way of wrapping long lines is by using Python’s implied line continuation inside parentheses, brackets, and braces. 
Long lines can be broken over multiple lines by wrapping expressions in parentheses.
These should be used in preference to using a backslash for line continuation.

— From https://pep8.org/#maximum-line-length

print(f"deleted >{branch_name}< branch\n"
f"~~~> Deletion complete")
""" OR """
multiliner = (f"deleted >{branch_name}< branch\n"
f"~~~> Deletion complete")
print(multiliner)

sources

--

--

No responses yet