Sometimes, we want to overwrite the previous print to stdout in Python.
In this article, we’ll look at how to overwrite the previous print to stdout in Python.
How to overwrite the previous print to stdout in Python?
To overwrite the previous print to stdout in Python, we call print
with the end
argument set to a carriage return.
For instance, we write
for x in range(10):
print(x, end='\r')
print()
to call print
to print x
with a carriage return at the end as specified by
end='\r'
This will overwrite the previous print to stdout.
Conclusion
To overwrite the previous print to stdout in Python, we call print
with the end
argument set to a carriage return.