Python program to print over the same line

Learn to print over the same line in Python.

Python program to print over the same line

Let’s say you have a bunch of print statements.

Code

print("Pylenin")
print("loves")
print("Python")

The output will look like below.

Output

Pylenin
loves
Python

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.

Code

print("Pylenin", end="\r")
print("loves", end="\r")
print("Python")

Output

Pythonn

Even though the first 2 print statements are executed, the carriage returns makes the next stdout line start at the beginning of the current line.

Also, carriage return will only replace the number of characters contained in the print statement. So you have an extra n at the end.

Learn about end, sep and flush parameters of print() function here.

Diving deeper into Python print
Learn to print relevant messages to various outputs using Python. Examples for both Python 2 and Python 3 are provided.

Subscribe to Pylenin

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
jamie@example.com
Subscribe