# Proper indentation for Python multiline strings

## The Question :

474 people think this question is useful

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.

The Question Comments :
• Docstrings are treated specially: any indent of the first line is removed; the smallest common indent taken over all other non-blank lines is removed from them all. Other than that, multiline string literals in Python are unfortunately what-you-see-is-what-you-get in terms of whitespace: all characters between the string delimiters become part of the string, including indentation that, with Python reading instincts, looks like it should be measured from the indent of the line where the literal starts.
• @EvgeniSergeev The processing tool performs this task (and that largely depends on your choice of processing tool). method.__doc__ isn’t modified by Python itself any more than any other str literal.

## The Answer 1

470 people think this answer is useful

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:

def trim(docstring):
if not docstring:
return ''
# Convert tabs to spaces (following the normal Python rules)
# and split into a list of lines:
lines = docstring.expandtabs().splitlines()
# Determine minimum indentation (first line doesn't count):
indent = sys.maxint
for line in lines[1:]:
stripped = line.lstrip()
if stripped:
indent = min(indent, len(line) - len(stripped))
# Remove indentation (first line is special):
trimmed = [lines[0].strip()]
if indent < sys.maxint:
for line in lines[1:]:
trimmed.append(line[indent:].rstrip())
# Strip off trailing and leading blank lines:
while trimmed and not trimmed[-1]:
trimmed.pop()
while trimmed and not trimmed[0]:
trimmed.pop(0)
# Return a single string:
return '\n'.join(trimmed)



## The Answer 2

278 people think this answer is useful

The 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!



## The Answer 3

65 people think this answer is useful

Use inspect.cleandoc like so:

def method():
string = inspect.cleandoc("""
line one
line two
line three""")



Relative indentation will be maintained as expected. As commented below, if you want to keep preceding empty lines, use textwrap.dedent. However that also keeps the first line break.

Note: It’s good practice to indent logical blocks of code under its related context to clarify the structure. E.g. the multi-line string belonging to the variable string.

## The Answer 4

23 people think this answer is useful

One option which seems to missing from the other answers (only mentioned deep down in a comment by naxa) is the following:

def foo():
string = ("line one\n"          # Add \n in the string
"line two"  "\n"      # Add "\n" after the string
"line three\n")



This will allow proper aligning, join the lines implicitly, and still keep the line shift which, for me, is one of the reasons why I would like to use multiline strings anyway.

It doesn’t require any postprocessing, but you need to manually add the \n at any given place that you want the line to end. Either inline or as a separate string after. The latter is easier to copy-paste in.

## The Answer 5

18 people think this answer is useful

Some more options. In Ipython with pylab enabled, dedent is already in the namespace. I checked and it is from matplotlib. Or it can be imported with:

from matplotlib.cbook import dedent



In documentation it states that it is faster than the textwrap equivalent one and in my tests in ipython it is indeed 3 times faster on average with my quick tests. It also has the benefit that it discards any leading blank lines this allows you to be flexible in how you construct the string:

"""
line 1 of string
line 2 of string
"""

"""\
line 1 of string
line 2 of string
"""

"""line 1 of string
line 2 of string
"""



Using the matplotlib dedent on these three examples will give the same sensible result. The textwrap dedent function will have a leading blank line with 1st example.

Obvious disadvantage is that textwrap is in standard library while matplotlib is external module.

Some tradeoffs here… the dedent functions make your code more readable where the strings get defined, but require processing later to get the string in usable format. In docstrings it is obvious that you should use correct indentation as most uses of the docstring will do the required processing.

When I need a non long string in my code I find the following admittedly ugly code where I let the long string drop out of the enclosing indentation. Definitely fails on “Beautiful is better than ugly.”, but one could argue that it is simpler and more explicit than the dedent alternative.

def example():
long_string = '''\
Lorem ipsum dolor sit amet, consectetur adipisicing
elit, sed do eiusmod tempor incididunt ut labore et
dolore magna aliqua. Ut enim ad minim veniam, quis
nostrud exercitation ullamco laboris nisi ut aliquip.\
'''
return long_string

print example()



## The Answer 6

6 people think this answer is useful

If you want a quick&easy solution and save yourself from typing newlines, you could opt for a list instead, e.g.:

def func(*args, **kwargs):
string = '\n'.join([
'first line of very long string and',
'second line of the same long thing and',
'third line of ...',
'and so on...',
])
print(string)
return



## The Answer 7

4 people think this answer is useful

I prefer

    def method():
string = \
"""\
line one
line two
line three\
"""



or

    def method():
string = """\
line one
line two
line three\
"""



## The Answer 8

3 people think this answer is useful

My two cents, escape the end of line to get the indents:

def foo():
return "{}\n"\
"freq: {}\n"\
"temp: {}\n".format( time, freq, temp )



## The Answer 9

1 people think this answer is useful

I came here looking for a simple 1-liner to remove/correct the identation level of the docstring for printing, without making it look untidy, for example by making it “hang outside the function” within the script.

Here’s what I ended up doing:

import string
def myfunction():

"""
line 1 of docstring
line 2 of docstring
line 3 of docstring"""

print str(string.replace(myfunction.__doc__,'\n\t','\n'))[1:]



Obviously, if you’re indenting with spaces (e.g. 4) rather than the tab key use something like this instead:

print str(string.replace(myfunction.__doc__,'\n    ','\n'))[1:]



And you don’t need to remove the first character if you like your docstrings to look like this instead:

    """line 1 of docstring
line 2 of docstring
line 3 of docstring"""

print string.replace(myfunction.__doc__,'\n\t','\n')



## The Answer 10

0 people think this answer is useful

For strings you can just after process the string. For docstrings you need to after process the function instead. Here is a solution for both that is still readable.

class Lstrip(object):
def __rsub__(self, other):
import re
return re.sub('^\n', '', re.sub('\n$', '', re.sub('\n\s+', '\n', other))) msg = ''' Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. ''' - Lstrip() print msg def lstrip_docstring(func): func.__doc__ = func.__doc__ - Lstrip() return func @lstrip_docstring def foo(): ''' Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. ''' pass print foo.__doc__  ## The Answer 11 0 people think this answer is useful The first option is the good one – with indentation included. It is in python style – provides readability for the code. To display it properly: print string.lstrip()  ## The Answer 12 0 people think this answer is useful I’m having a similar issue, code got really unreadable using multilines, I came out with something like print("""aaaa """ """bbb """)  yes, at beginning could look terrible but the embedded syntax was quite complex and adding something at the end (like ‘\n”‘) was not a solution ## The Answer 13 0 people think this answer is useful You can use this function trim_indent. import re def trim_indent(s: str): s = re.sub(r'^\n+', '', s) s = re.sub(r'\n+$', '', s)
spaces = re.findall(r'^ +', s, flags=re.MULTILINE)
if len(spaces) > 0 and len(re.findall(r'^[^\s]', s, flags=re.MULTILINE)) == 0:
s = re.sub(r'^%s' % (min(spaces)), '', s, flags=re.MULTILINE)
return s

print(trim_indent("""

line one
line two
line three
line two
line one

"""))



Result:

"""
line one
line two
line three
line two
line one
"""



## The Answer 14

-1 people think this answer is useful

It depends on how you want the text to display. If you want it all to be left-aligned then either format it as in the first snippet or iterate through the lines left-trimming all the space.