Summary
Jump straight down and look at Examples 1 and 4 below.
Full answer:
I just barely learned about the Python textwrap
module, with its really handy textwrap.dedent()
function, and considering it's been around since Python 2.7, I can't believe it isn't more popular!
Using textwrap.dedent()
around a multi-line string solves all of the problems of my previous answer!
textwrap.dedent(text)
Remove any common leading whitespace from every line in text.
This can be used to make triple-quoted strings line up with the left edge of the display, while still presenting them in the source code in indented form.
Note that tabs and spaces are both treated as whitespace, but they are not equal: the lines " hello"
and "\thello"
are considered to have no common leading whitespace.
Lines containing only whitespace are ignored in the input and normalized to a single newline character in the output.
For example:
def test():
# end first line with \ to avoid the empty line!
s = '''\
hello
world
'''
print(repr(s)) # prints ' hello\n world\n '
print(repr(dedent(s))) # prints 'hello\n world\n'
For all examples
import textwrap
Example 1
So, instead of this, as the most-upvoted answer states (this loses the nice, clean, indentation):
cmd = '''line {0}
line {1}
line {2}'''.format(1,2,3)
print(cmd)
Do this (and KEEP the nice, clean, indentation)!
import textwrap
cmd = textwrap.dedent('''\
line {0}
line {1}
line {2}''').format(1,2,3)
print(cmd)
OR, using Python3's new-and-improved "f" format strings instead of the .format()
method!:
import textwrap
var0 = 1
var1 = 2
var2 = 3
cmd = textwrap.dedent(f'''\
line {var0}
line {var1}
line {var2}''')
print(cmd)
Example 2
If you have many arguments to the format()
function, you can put them on multiple lines if desired. Notice that the format()
arguments take up two lines here:
cmd = textwrap.dedent('''\
line {0}
line {1}
line {2}
line {3}
line {4}
line {5}
line {6}
line {7}
line {8}
line {9}
line {10}
line {11}
line {12}
line {13}
line {14}
line {15}
line {16}
line {17}
line {18}
line {19}''').format(
1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
)
print(cmd)
And of course, if the format()
arguments are really long, you can put each argument on its own line too:
cmd = textwrap.dedent('''\
line {0}
line {1}
line {2}
line {3}
line {4}
line {5}
line {6}
line {7}
line {8}
line {9}
line {10}
line {11}
line {12}
line {13}
line {14}
line {15}
line {16}
line {17}
line {18}
line {19}''').format(
100000000000000000000000000000000000000000000000000000000000000000001,
100000000000000000000000000000000000000000000000000000000000000000002,
100000000000000000000000000000000000000000000000000000000000000000003,
100000000000000000000000000000000000000000000000000000000000000000004,
100000000000000000000000000000000000000000000000000000000000000000005,
100000000000000000000000000000000000000000000000000000000000000000006,
100000000000000000000000000000000000000000000000000000000000000000007,
100000000000000000000000000000000000000000000000000000000000000000008,
100000000000000000000000000000000000000000000000000000000000000000009,
100000000000000000000000000000000000000000000000000000000000000000010,
100000000000000000000000000000000000000000000000000000000000000000011,
100000000000000000000000000000000000000000000000000000000000000000012,
100000000000000000000000000000000000000000000000000000000000000000013,
100000000000000000000000000000000000000000000000000000000000000000014,
100000000000000000000000000000000000000000000000000000000000000000015,
100000000000000000000000000000000000000000000000000000000000000000016,
100000000000000000000000000000000000000000000000000000000000000000017,
100000000000000000000000000000000000000000000000000000000000000000018,
100000000000000000000000000000000000000000000000000000000000000000019,
100000000000000000000000000000000000000000000000000000000000000000020,
)
print(cmd)
Example 3
And instead of this, as I stated in my original answer (which keeps nice-looking indentation but is a bit tedious to use):
print("\n\n" +
"########################\n" +
"PRINT DOCSTRING DEMO:\n" +
"########################")
...you can now do this!--which allows my multi-line strings, when printed, to "line up with the left edge of the display, while still presenting them in the source code in indented form" (see official documentation):
# Note: use the `\` below to prevent the implicit newline right after it from being printed.
print(textwrap.dedent("""
########################
PRINT DOCSTRING DEMO:
########################\
"""))
Example 4
And instead of this, which has some ugly-looking lack-of-indentation right in the middle of it:
def printDocstrings1():
"""
Print all document strings for this module, then exit.
Params: NA
Returns: NA
"""
# A LITTLE BIT UGLY, BUT IT WORKS.
print("""
---------------------
Module Documentation:
---------------------
printDocstrings:{}
myFunc1:{}
class Math:{}
__init__:{}
add:{}
subtract:{}""".format(
printDocstrings1.__doc__,
myFunc1.__doc__,
Math.__doc__,
Math.__init__.__doc__,
Math.add.__doc__,
Math.subtract.__doc__))
...do this, which uses textwrap.dedent()
to keep nice-looking indentation all throughout!:
def printDocstrings2():
"""
Print all document strings for this module, then exit.
Params: NA
Returns: NA
"""
# MUCH CLEANER! Now I can have the proper indentation on the left withOUT
# it printing that indentation!
print(textwrap.dedent("""\
---------------------
Module Documentation:
---------------------
printDocstrings:{}
myFunc1:{}
class Math:{}
__init__:{}
add:{}
subtract:{}""").format(
printDocstrings2.__doc__,
myFunc1.__doc__,
Math.__doc__,
Math.__init__.__doc__,
Math.add.__doc__,
Math.subtract.__doc__))
Run the code above
You can run my test code above in my eRCaGuy_hello_world GitHub repo here: textwrap_practice_1.py.
Run command:
./textwrap_practice_1.py
OR:
python3 textwrap_practice_1.py