Python print overwrite same line

Video Python print overwrite same line

Quick and simple.

Summary

  1. Print line, but end with a carriage return instead of a newline character. This moves the cursor back to the beginning of the printed line.
  2. 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.

Approach

By default, Python’s print statement ends each string that is passed into the function with a newline character, n. This behavior can be overridden with the function’s end parameter, which is the core of this method.

Rather than ending the output with a newline, we use a carriage return.

When we end a print statement with a carriage return, we essentially move the cursor back to the beginning of the printed line, instead of to the next line. Then, if we print another string, the text is printed “on top” of the previous string. In Python, a carriage return is indicated with r.

Read more: Forever and always tattoo designs

? Originally the term “carriage return” referred to a mechanism or lever on a typewriter that was operated after typing a line of text, which aligned the type element of the machine back to the left side of the paper.

Example Case

Say we have a simple string that we want to print character by character, one character every 0.5 seconds:

Example case (no overwrites yet).

Using the default print statement, this looks like so:

Example case (no overwrites yet).

Now look what happens if we end each print statement with a carriage return instead:

Method 1: Example case (with carriage return).

All characters are printed on the same line!

Method 1 in practice.

Read more: Eyebrow threading for men

Easy right? Unfortunately, there is a caveat. Because we essentially output each consecutive print statement on top of the previous output, it is not feasible to print a line that is shorter than the line before.

See what happens if we print ‘done’ at the end of the loop:

Method 1 caveat: printing shorter lines.

We can prevent this by clearing the previous line before we print a shorter string.

The ANSI sequence on line 6 indicates that the line where the cursor is located should be erased (note the r at the end of the for-loop). By using the LINE_CLEAR variable as end parameter we ensure that no additional newline character is printed.

Method 1: working example.

Read more: Four principles of experimental design

Slightly more complex, but cleaner output and more flexible.

Summary

  1. Print line (ending with a newline by default).
  2. Just before the next print statement: move the cursor up and clear the line. This action can be repeated to undo multiple printed lines.
  3. Print your next line.

Approach

In this method we do not alter the end parameter of the print statement that is used to display our output. Instead, we add an extra print statement that clears the previous line using two ANSI escape codes:

Method 2: working example.
  1. The ANSI code that is assigned to LINE_UP indicates that the cursor should move up a single line.
  2. The ANSI code that is assigned to LINE_CLEAR erases the line where the cursor is located (also used in the first method).

Note that we have altered the end parameter of the extra print statement to prevent the default newline character from being printed.

Method 2 in practice.

Functionally, this method provides two advantages over the first method:

  1. You don’t have to worry about the length of the next line.
  2. The cursor does not visually interfere with the printed lines.

Neat, right?

We have busy lives. I get it!

Related Posts