Quick and simple.
- 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.
- 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. 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.
Say we have a simple string that we want to print character by character, one character every 0.5 seconds:
Using the default print statement, this looks like so:
Now look what happens if we end each print statement with a carriage return instead:
All characters are printed on the same line!
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:
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.
Read more: Four principles of experimental design
Slightly more complex, but cleaner output and more flexible.
- Print line (ending with a newline by default).
- Just before the next print statement: move the cursor up and clear the line. This action can be repeated to undo multiple printed lines.
- Print your next line.
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:
- The ANSI code that is assigned to LINE_UP indicates that the cursor should move up a single line.
- 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.
Functionally, this method provides two advantages over the first method:
- You don’t have to worry about the length of the next line.
- The cursor does not visually interfere with the printed lines.