Categories
Python Answers

How to overwrite the previous print to stdout in Python?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *