What is the proper indentation for Python multiline strings within a function?
def method(): string = """line one line two line three"""
or
def method(): string = """line one line two line three"""
or something else?
It looks kind of weird to have the string hanging outside the function in the first example.
You probably want to line up with the
"""
def foo(): string = """line one line two line three"""
Since the newlines and spaces are included in the string itself, you will have to postprocess it. If you don't want to do that and you have a whole lot of text, you might want to store it separately in a text file. If a text file does not work well for your application and you don't want to postprocess, I'd probably go with
def foo(): string = ("this is an " "implicitly joined " "string")
If you want to postprocess a multiline string to trim out the parts you don't need, you should consider the
textwrap module or the technique for postprocessing docstrings presented in PEP 257:
textwrap.dedent function allows one to start with correct indentation in the source, and then strip it from the text before use.
The trade-off, as noted by some others, is that this is an extra function call on the literal; take this into account when deciding where to place these literals in your code.
import textwrap def frobnicate(param): """ Frobnicate the scrognate param. The Weebly-Ruckford algorithm is employed to frobnicate the scrognate to within an inch of its life. """ prepare_the_comfy_chair(param) log_message = textwrap.dedent("""\ Prepare to frobnicate: Here it comes... Any moment now. And: Frobnicate!""") weebly(param, log_message) ruckford(param)
The trailing
\ in the log message literal is to ensure that line break isn't in the literal; that way, the literal doesn't start with a blank line, and instead starts with the next full line.
The return value from
textwrap.dedent is the input string with all common leading whitespace indentation removed on each line of the string. So the above
log_message value will be:
Prepare to frobnicate: Here it comes... Any moment now. And: Frobnicate!