Here's code for Python 3.x:

print(os.path.getsize(file_name)/1024+'KB / '+size+' KB downloaded!', end='\r')

The end= keyword is what does the work here -- by default, print() ends in a newline (\n) character, but this can be replaced with a different string. In this case, ending the line with a carriage return instead returns the cursor to the start of the current line. Thus, there's no need to import the sys module for this sort of simple usage. print() actually has a number of keyword arguments which can be used to greatly simplify code.

To use the same code on Python 2.6+, put the following line at the top of the file:

from __future__ import print_function
Answer from Kudzu on Stack Overflow
🌐
Reddit
reddit.com › r/learnpython › how to completely overwrite previous print with a new one?
r/learnpython on Reddit: How to completely overwrite previous print with a new one?
October 23, 2023 -

I have to print a progress status, so update a print with a new one. I read many post that says you have to use "\r", so I tried this:

print(string, end="\r", flush=True)

The problem is that when the previous printed string is bigger than the second one, some stuff of old print will reamins in the new print.

Here's the full code:

import time

long_string = "AAAAA" short_string = "BB" c = 0 while True: if c % 2 == 0: print(long_string, end="\r", flush=True) else: print(short_string, end="\r", flush=True) c += 1 time.sleep(1)

I would expect that in the if it would print "AAAAA" and in the else it would print just "BB", but instead, in the else, it prints "BBAAA" because the "A" remains from previous print. Here's what I mean https://i.stack.imgur.com/rE9ep.png. I would like it would be just "BB" instead of "BBAAA".

I saw this post answer https://stackoverflow.com/questions/45263205/python-how-to-print-on-same-line-clearing-previous-text so I tried to use

os.sys.stdout.write('\033[K' + long_string + '\r')

instead of print, but I still have the same problem.

How can I completely delete previous clean and make a new one without traces of the old one?

Discussions

python - How to overwrite the previous print to stdout? - Stack Overflow
What I would like to do is instead of printing a newline, I want to replace the previous value and overwrite it with the new value on the same line. ... As a side note, ensure you get the answer which able to flush entire line if previous line longer than current line. ... One way is to use the carriage return ('\r') character to return to the start of the line without advancing to the next line. ... In the latter two (Python ... More on stackoverflow.com
🌐 stackoverflow.com
How to print progress on the same line in Python? - Ask a Question - TestMu AI Community
How can I print on the same line in Python? I want to create a script that outputs progress on the same line, like this: Installing XXX... [DONE] Currently, I print Installing XXX... first, followed by [DONE] on the next line. How can I adjust the script to print both messages on the same line, one at a time, without overwriting ... More on community.testmuai.com
🌐 community.testmuai.com
0
December 4, 2024
Python: print and overwrite the same line reading from a file - Stack Overflow
I know that this question has been asked before but after a long search I've couldnt find any solution that helps me. currently, I'm trying to open a file given by a user, read the file content and... More on stackoverflow.com
🌐 stackoverflow.com
How Do I Print Something On The Same Line?

You really should ask questions like this in r/learnpython

In Python 3, you can supply an 'end' argument to the print function to replace the default ending character, a newline. This can be an empty string.

In Python 2, you can use system.stdout.write to have precise control of output. You can do this in Python 3 as well, of course.

More on reddit.com
🌐 r/Python
6
0
April 1, 2015
🌐
ITNEXT
itnext.io › overwrite-previously-printed-lines-4218a9563527
Overwrite Previously Printed Lines | by Thijmen Dam | ITNEXT
February 28, 2022 - Print another line, but because the cursor is now at the beginning of the previous line, the new output will be printed on top of the previous line. By default, Python’s print statement ends each string that is passed into the function with a newline character, \n.
🌐
Quora
quora.com › How-do-I-overwrite-previous-printed-lines-in-Python-3-6-5
How to overwrite previous printed lines in Python 3.6.5 - Quora
The cleanest approach, if possible, is to use \r, which goes back to the beginning of the line. Your next print will simply start from where the previous one started. ... Overwriting previously printed lines in a terminal requires control sequences ...
🌐
LinkedIn
linkedin.com › pulse › python-program-print-over-same-line-lenin-mishra-
Python program to print over the same line - Lenin Mishra
August 23, 2021 - However, if you want to print over and over the same line in Python, you have to use the carriage return \r symbol with the end argument. ... Even though the first 2 print statements are executed, the carriage return makes the next stdout line ...
🌐
Kyletk
kyletk.com › post › 18 › python-overwrite-output-line
KyleTK - Python Overwrite Output Line
By default it is \n which is a ... to the beginning of the line we can specify the end to be \r which will bring the cursor back to the beginning of the line and anything printed after will overwrite what was previously there....
🌐
Finxter
blog.finxter.com › how-to-overwrite-the-previous-print-to-stdout-in-python
How to Overwrite the Previous Print to Stdout in Python? – Be on the Right Side of Change
May 22, 2022 - Summary: The most straightforward way to overwrite the previous print to stdout is to set the carriage return ('\r') character within the print statement as print(string, end = "\r"). This returns the next stdout line to the beginning of the line without proceeding to the next line.
Find elsewhere
🌐
YouTube
youtube.com › fabio musanni - programming channel
How to Overwrite Previous Printed Line in Python | Print Without Newline | Carriage Return - YouTube
💻 *Get my Source Codes and support the channel* ❤️: https://www.buymeacoffee.com/fabiomusanni/extras⬇️ *LEARN ON THE BEST LEARNING PLATFORMS (LINKS BELOW)* ...
Published   January 9, 2021
Views   24K
🌐
Raspberry Pi Forums
forums.raspberrypi.com › board index › programming › python
Overwrite previous output Help!(Python 2.7.9) - Raspberry Pi Forums
That works perfectly on my laptop running python 2.7.6 I assume you're running this as a script and not through IDLE (although I don't know if that would make a difference). RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc. ... James, From your text I gather that you want your output to be printed at the beginning of the same line every time.
🌐
Raspberry Pi Forums
forums.raspberrypi.com › board index › programming › python
Printing without 'next line' and overwrite previous data - Raspberry Pi Forums
October 10, 2015 - Hi, I am writing a simple python test program that repeatedly reads a string value (from a file) and i want to print that value to the same line on the console (using Putty) and overwriting the previous output to that line on the screen. So basically i want to print with only a 'return' and ...
🌐
Python Forum
python-forum.io › thread-7367.html
Overwrite last printed line
Hi everyone, I'm quite new to Python programming, which is why this thread is located in the Homework section: it's not because of a teacher that I'm restricted in my use of syntax, it's because of my basic knowledge. I'm trying to program a clock ...
🌐
CopyProgramming
copyprogramming.com › howto › python-python-overwrite-output-on-same-line
Python Overwrite Output on Same Line: Complete Guide with Latest Techniques for 2026 - Python python overwrite output on same line
January 4, 2026 - The carriage return character (\r) is the simplest and most direct way to overwrite output on the same line. When printed, it moves the cursor to the start of the current line without advancing to a new line, allowing subsequent text to overwrite the previous content.
Top answer
1 of 16
257

Simple Version

One way is to use the carriage return ('\r') character to return to the start of the line without advancing to the next line.

Python 3

for x in range(10):
    print(x, end='\r')
print()

Python 2.7 forward compatible

from __future__ import print_function
for x in range(10):
    print(x, end='\r')
print()

Python 2.7

for x in range(10):
    print '{}\r'.format(x),
print

Python 2.0-2.6

for x in range(10):
    print '{0}\r'.format(x),
print

In the latter two (Python 2-only) cases, the comma at the end of the print statement tells it not to go to the next line. The last print statement advances to the next line so your prompt won't overwrite your final output.

Line Cleaning

If you can’t guarantee that the new line of text is not shorter than the existing line, then you just need to add a “clear to end of line” escape sequence, '\x1b[1K' ('\x1b' = ESC):

for x in range(75):
    print('*' * (75 - x), x, end='\x1b[1K\r')
print()

Long Line Wrap

All these methods assume you’re not writing more than the length of the line. The carriage return only returns to the start of the current line, so if your output is longer than a line, you’ll only erase the last line.

If this is enough of a problem that you need to control it, you can disable line wrapping to keep the cursor from wrapping to the next line. (Instead, the cursor sticks to the end of the line, and successive characters overwrite.)

Line wrap is disabled with print('\x1b[7l', end='') and re-enabled with print('\x1b[7h', end=''). Note that there is no automatic re-enable of line wrap at any point: don’t leave the terminal broken if an exception ends your program!

2 of 16
114

Since I ended up here via Google but am using Python 3, here's how this would work in Python 3:

for x in range(10):
    print("Progress {:2.1%}".format(x / 10), end="\r")

Related answer here: How can I suppress the newline after a print statement?

🌐
Iditect
iditect.com › faq › python › output-to-the-same-line-overwriting-previous-output-in-python.html
Output to the same line overwriting previous output in python?
To output text to the same line and overwrite the previous output in Python, you can use the '\r' (carriage return) character in combination with the sys.stdout.write() function or print() function with the end parameter. This is often used for creating dynamic progress bars or updating text ...
🌐
TestMu AI Community
community.testmuai.com › ask a question
How to print progress on the same line in Python? - Ask a Question - TestMu AI Community
December 4, 2024 - How can I print on the same line in Python? I want to create a script that outputs progress on the same line, like this: Installing XXX... [DONE] Currently, I print Installing XXX... first, followed by [DONE] on the next line. How can I adjust the script to print both messages on the same line, one at a time, without overwriting ...
🌐
sqlpey
sqlpey.com › python › python-print-same-line-techniques
Python Print to Same Line Techniques: Overwriting Console Output
November 4, 2025 - This requires tracking the length of the last message printed. last_len = 0 def update_status(message: str): global last_len # 1. Overwrite old message with spaces # Use flush=True if output buffer issues arise (see Solution D) print(' ' * last_len, end='\r') # 2.
🌐
TecAdmin
tecadmin.net › how-to-print-in-same-line-in-python
How to Display Output in the Same Line in Python – TecAdmin
April 26, 2025 - It’s important to note that the sys.stdout.write() method and the \r character are the most direct methods for controlling the output in the same line, as they allow you to write directly to the standard output stream. On the other hand, the end argument in the print() function and the flush ...
🌐
Stack Overflow
stackoverflow.com › questions › 69129649 › python-print-and-overwrite-the-same-line-reading-from-a-file
Python: print and overwrite the same line reading from a file - Stack Overflow
Trying: xxx --> replace the xxx ... sys.stdout.write('trying: %s' % line) sys.stdout.flush() time.sleep(0.3) ... Have you try print(line, end="\r") , it should work for string....
🌐
DNMTechs
dnmtechs.com › overwriting-output-on-the-same-line-in-python-3
Overwriting Output on the Same Line in Python 3 – DNMTechs – Sharing and Storing Technology Knowledge
This feature is widely used in various applications and is supported by numerous libraries and frameworks in the Python ecosystem. One common use case for overwriting output on the same line is to create a progress bar. Here’s an example: import time def progress_bar(total, current): progress ...