To answer your question, they are essentially the same. It is really a matter of readability, and thus personal preference.
However, there is a more convenient way to do it, which is to use multi line strings. They are delimited by """
, and are essentially strings that are capable of spanning multiple lines.
For example,
print("Hello,
world!")
would throw an error saying EOL while scanning string literal
, whereas
print("""Hello,
world!""")
is fine and prints this:
Hello,
World!
as it is supposed to.
Clarification: It is not the same as using the line continuation character(\
). That only goes to the next line without breaking the string, to help the programmer read their code. It does not include the newline.
This:
print("Hello,\
world!")
Is not the same as this:
print("""Hello,
world!""")
While it is true they are both valid, they have different results. The former would print:
Hello,world!
While the latter would print
Hello,
world!
EDIT:
When using this for a docstring, there is the concern that indentation meant for human readability will interfere with it by adding in extra tabs. Example:
# Less easily readable
def test():
"""Docstring
Docstring"""
pass
# More easily readable
def otherTest():
"""Docstring
Docstring"""
pass
The thing is that these two docstrings produce the exact same result. Python ignores the leading whitespace.
Source:
Proper indentation for Python multiline strings