Python 如何用新的打印行替换打印行?

Python 如何用新的打印行替换打印行?,python,python-3.x,Python,Python 3.x,我要做的是用新的打印语句替换打印语句。用外行的话说,我希望控制台打印下载…,然后用下载…完成一旦下载完成。我试过了,但它只是打印一些垃圾,然后在新行上打印语句。我正在使用Python3。提前谢谢 使用end=”“在第一次打印中,end的默认值是一个新行,但您可以通过传递自己的值来更改它: print("Downloading...",end="") #your code here print("Done!") 输出: Downloading...Done! 有关打印的帮助信息: In [3]

我要做的是用新的打印语句替换打印语句。用外行的话说,我希望控制台打印
下载…
,然后用
下载…完成一旦下载完成。我试过了,但它只是打印一些垃圾,然后在新行上打印语句。我正在使用Python3。提前谢谢

使用
end=”“
在第一次
打印
中,
end
的默认值是一个
新行
,但您可以通过传递自己的值来更改它:

print("Downloading...",end="")
#your code here
print("Done!")
输出

Downloading...Done!
有关打印的帮助信息

In [3]: print?
Type:       builtin_function_or_method
String Form:<built-in function print>
Namespace:  Python builtin
Docstring:
print(value, ..., sep=' ', end='\n', file=sys.stdout)

Prints the values to a stream, or to sys.stdout by default.
Optional keyword arguments:
file: a file-like object (stream); defaults to the current sys.stdout.
sep:  string inserted between values, default a space.
end:  string appended after the last value, default a newline.
[3]中的
:打印?
类型:内置函数或方法
字符串形式:
名称空间:Python内置
文档字符串:
打印(值,…,sep='',end='\n',file=sys.stdout)
默认情况下,将值打印到流或sys.stdout。
可选关键字参数:
文件:类似文件的对象(流);默认为当前sys.stdout。
sep:值之间插入的字符串,默认为空格。
结束:最后一个值后追加的字符串,默认为换行符。
一个简单的例子:

import time
print("Downloading... ", end='')
time.sleep(3)
print("done.")
在使用“\r”之前,您还可以替换printet行的一部分:

当然,这只适用于不在回车符之前打印换行符的情况

print ("Print this line, and print a newline")
print ("Print this line, but not a newline", end="")

如果您想要一个交互式的
,也就是说,它会增长,您可以这样做。只需在
while
中用更合适的方法更改条件,甚至在循环中使用
while True
if
break

>>> import time
>>> def dotdotdot():
...     print("Downloading", end="")
...     a = 0
...     while a < 10:
...         print(".", end="")
...         time.sleep(1)
...         a += 1
...     print("done!")
...
>>> dotdotdot()
Downloading..........done!
导入时间 >>>def dotdot(): ... 打印(“下载”,end=”“) ... a=0 ... 而a<10: ... 打印(“.”,end=“”) ... 时间。睡眠(1) ... a+=1 ... 打印(“完成!”) ... >>>dotdot() 下载…………完成!
这是否在Windows控制台上?
>>> import time
>>> def dotdotdot():
...     print("Downloading", end="")
...     a = 0
...     while a < 10:
...         print(".", end="")
...         time.sleep(1)
...         a += 1
...     print("done!")
...
>>> dotdotdot()
Downloading..........done!