python - 如何打印输出并保持在同一行(Python)?

标签 python python-3.x output

我有一个跨越多行的字符串,导致终端滚动到最新打印的行。但我想留在第一行,这样我就能看到第一行,但看不到最新的。那可能吗? 例如:

for i in range(100):
    print(f"hello {i}")

我想看到 hello 0 但输出应该保持相同的形状

最佳答案

如果您使用的是 GNU,则可以将 defscrollback 设置为 100。否则,这应该有帮助:

class More(object):
def __init__(self, num_lines):
    self.num_lines = num_lines
def __ror__(self, other):
    s = str(other).split("\n")
    for i in range(0, len(s), self.num_lines):
        print(*s[i: i + self.num_lines], sep="\n")
        input("Press <Enter> for more")
more = More(num_lines=30)  
"\n".join(map(str, range(100))) | more

关于python - 如何打印输出并保持在同一行(Python)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59986526/

相关文章:

Python 2 与 3 : consistent results with getting a byte from byte string

python - 猜字游戏 -- 这还能写得更好吗?

python - 返回函数的输出与打印它有何不同?

c - 打印到 C 中的文件

c - c中带有百分比字符的奇怪格式说明符

python - 如何将一个类中所有对象共享的变量增加 1?

python - 在 Python 中监控并发性(跨进程共享对象)

python - SMTP 服务器断开连接 : please run connect() first

python - 如何从原始文本中解析名称

python 带有可选参数的假设检验