简体   繁体   English

如何在 Python 中打印动态字符串?

[英]How to print dynamic string in Python?

How to print dynamic string in Python?如何在 Python 中打印动态字符串?

Example:例子:

n1 = 1
while n1 < 20:
    print(f"{n1} word from 20 scanned")
    n1 += 1

Don't print retry text !不要打印重试文本!

I want to update n1 value我想更新 n1 值

What you might be looking for is \r , or carriage return, it moves the cursor in the terminal to the start of the line.您可能正在寻找的是\r或回车,它将终端中的 cursor 移动到行首。 That way, the text can be rewritten when new text is printed.这样,在打印新文本时可以重写文本。

n1 = 1
while n1 < 20:
    print(f"\r{n1} word from 20 scanned", end="")
    n1 += 1

Explanation:解释:

| <- cursor
1 word from 20 scanned| <- write the first line  
|1 word from 20 scanned <- cursor moves to the start(\r)  
2 word from 20 scanned| <- rewrite  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM