TLDR;
直接跳下来,查看下面的示例1和4。
完整答案:
上周(2020年10月),我才刚刚了解到Pythontextwrap模块,它具有非常方便的textwrap.dedent()功能,并且考虑到自python 2.7以来就已经存在,我不敢相信它并不流行!
textwrap.dedent()在多行字符串周围使用可解决我先前回答的所有问题!
textwrap.dedent(text)
从文本的每一行中删除所有常见的前导空格。
这可以使三引号字符串与显示的左边缘对齐,同时仍将它们以缩进形式显示在源代码中。
请注意,制表符和空格都被视为空格,但它们并不相等:行" hello"和"\thello"被认为没有共同的前导空白。
仅包含空格的行在输入中被忽略,并在输出中标准化为单个换行符。
例如:
def test():
s = '''\
hello
world
'''
print(repr(s))
print(repr(dedent(s)))
对于所有示例
import textwrap
例子1
因此,而不是this,正如最受好评的答案所言(这会失去美观,简洁的缩进):
cmd = '''line {0}
line {1}
line {2}'''.format(1,2,3)
print(cmd)
做到这一点(并保持良好,干净,缩进的状态)!
cmd = textwrap.dedent('''\
line {0}
line {1}
line {2}''').format(1,2,3)
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