TLDR;
直接跳下来看看下面的示例 1 和示例 4。
完整答案:
上周(2020 年 10 月)我刚刚才了解Pythontextwrap
模块textwrap.dedent()
,它具有非常方便的功能,考虑到它自 Python 2.7 以来就已经存在,我不敢相信它没有更受欢迎!
使用textwrap.dedent()
多行字符串解决了我之前回答的所有问题!
textwrap.dedent(text)
从文本的每一行中删除任何常见的前导空格。
这可用于使三引号字符串与显示的左边缘对齐,同时仍以缩进形式在源代码中显示它们。
请注意,制表符和空格都被视为空格,但它们不相等:行" hello"
和"\thello"
被认为没有共同的前导空格。
仅包含空格的行在输入中被忽略,并在输出中标准化为单个换行符。
例如:
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'
对于所有示例
import textwrap
示例 1
因此,而不是 this,正如最受支持的答案所述(这失去了漂亮、干净、缩进):
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
而不是 this,正如我在原始答案中所说的那样(缩进看起来很漂亮,但使用起来有点乏味):
print("\n\n" +
"########################\n" +
"PRINT DOCSTRING DEMO:\n" +
"########################")
...你现在可以做到这一点!--这允许我的多行字符串在打印时“与显示器的左边缘对齐,同时仍以缩进形式在源代码中显示它们”(参见官方文档):
# Note: use the `\` below to prevent the implicit newline right after it from being printed.
print(textwrap.dedent("""
########################
PRINT DOCSTRING DEMO:
########################\
"""))
示例 4
而不是 this,它的中间有一些难看的缺乏缩进:
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__))
...这样做,用于textwrap.dedent()
始终保持美观的缩进!:
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__))
运行上面的代码
您可以在我的eRCaGuy_hello_world GitHub存储库中运行我上面的测试代码: textwrap_practice_1.py。
运行命令:
./textwrap_practice_1.py
或者:
python3 textwrap_practice_1.py