【发布时间】:2022-01-04 09:23:22
【问题描述】:
例子:
def print_text():
print("Here's some text")
print("Here's some more text")
print("Here's the rest of the text")
print_text()
# Hypothetical command that indents all of the following text
start_indenting()
print_text()
# Hypothetical command that stops indenting.
stop_indenting()
print_text()
期望的输出:
Here's some text
Here's some more text
Here's the rest of the text
Here's some text
Here's some more text
Here's the rest of the text
Here's some text
Here's some more text
Here's the rest of the text
我正在寻找能够缩进所有文本而不实际更改文本或命令的东西。我不知道我将如何实现这一目标。考虑到我在使用它的程序中的方法有大量的打印语句,编辑给定方法(print_text)中的每个打印语句将是最后的手段。我查看了 textwrap 但它无法满足我的需求。
【问题讨论】:
-
@mkrieger1 我有,但遗憾的是它无法从我所看到的方法中缩进文本。我考虑过用它尝试一些东西,但没有用。
-
它可以缩进任何文本。
-
我知道,但它没有我正在寻找的 start_indenting() 和 stop_indenting() 函数
-
所以你的问题不是如何实际缩进文本,而是如何管理缩进级别大于上下文之外的上下文。