摘要
直接跳到下面的示例1和示例4。
完整答案:
我刚刚了解了Python的textwrap模块,它有一个非常方便的textwrap.dedent()
函数,并且考虑到自Python 2.7以来一直存在,我无法相信它不更受欢迎!
在多行字符串周围使用textwrap.dedent()
可以解决所有我之前回答中的问题!
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():
s = '''\
hello
world
'''
print(repr(s))
print(repr(dedent(s)))
对于所有示例
import textwrap
例子 1
所以,不要像这样做,如最受欢迎的答案所述(这会导致缩进不整齐):
cmd = '''line {0}
line {1}
line {2}'''.format(1,2,3)
print(cmd)
这样做(并保持漂亮、整洁的缩进)!
import textwrap
cmd = textwrap.dedent('''\
line {0}
line {1}
line {2}''').format(1,2,3)
print(cmd)
或者,使用Python3的新版改良型“f”格式字符串,而不是.format()
方法!:
import textwrap
var0 = 1
var1 = 2
var2 = 3
cmd = textwrap.dedent(f'''\
line {var0}
line {var1}
line {var2}''')
print(cmd)
例子 2
如果你有很多参数需要传递给 format()
函数,你可以将它们分成多行写,如果需要的话。注意这里的 format()
参数占用了两行:
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)
当然,如果 format()
的参数非常长,您也可以将每个参数放在自己的一行中:
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)
示例3
与此不同的是,如我在原回答中所述(该方法保持了良好的缩进格式,但使用起来有些繁琐):
print("\n\n" +
"########################\n" +
"PRINT DOCSTRING DEMO:\n" +
"########################")
现在你可以这样做!--这允许我的多行字符串在打印时与显示的左边缘对齐,同时仍以缩进形式在源代码中呈现(请参见官方文档):
print(textwrap.dedent("""
########################
PRINT DOCSTRING DEMO:
########################\
"""))
例子4
相比于这种有一些丑陋的缺少缩进的东西,可以考虑使用下面的替代方法:
def printDocstrings1():
"""
Print all document strings for this module, then exit.
Params: NA
Returns: NA
"""
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__))
...做这个,它使用 textwrap.dedent()
来保持整个缩进看起来漂亮!
def printDocstrings2():
"""
Print all document strings for this module, then exit.
Params: NA
Returns: NA
"""
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__))
运行以上代码
你可以在我的eRCaGuy_hello_world GitHub存储库中运行我上面的测试代码,链接: textwrap_practice_1.py.
运行命令:
./textwrap_practice_1.py
或:
python3 textwrap_practice_1.py
'''string here'''
技术是一种可靠的技术,而且效果很好,但当您不得不将除第一行以外的所有内容左对齐并且源代码缩进了几个级别时,它会创建一些非常丑陋的源代码。为了获得漂亮且易于查看的源代码和良好的对齐方式,请改用我在这里提供的技术:https://dev59.com/t2gu5IYBdhLWcg3w6bKo#54564926。当然,它也有其缺点,但对于少于一个长段落的文本,我更喜欢使用这种技术而不是使用''' '''
技术。 - Gabriel Staples