DEV Community

PyLenin
PyLenin

Posted on

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")
Enter fullscreen mode Exit fullscreen mode

The output will look like below.

Output

Pylenin
loves
Python
Enter fullscreen mode Exit fullscreen mode

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.

Code

print("Pylenin", end="\r")
print("loves", end="\r")
print("Python")
Enter fullscreen mode Exit fullscreen mode

Output

Pythonn
Enter fullscreen mode Exit fullscreen mode

Even though the first 2 print statements are executed, the carriage return 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. That is the reason, you have an extra n at the end.

This is one of the applications of print in Python. If you are curious to know more, check out my blog on the various aspects of printing in Python.

AWS GenAI LIVE image

Real challenges. Real solutions. Real talk.

From technical discussions to philosophical debates, AWS and AWS Partners examine the impact and evolution of gen AI.

Learn more

Top comments (0)

AWS Q Developer image

Your AI Code Assistant

Ask anything about your entire project, code and get answers and even architecture diagrams. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Start free in your IDE

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay