Skip to content

How to print in same line in Python?

One annoying thing about print() function is, it automatically prints a newline ‘\n’ at the end of the line!

print() function was added in Python 3. Until Python 2, print() was a statement.

Python 3.x

Add an extra argument to the print() function .

You can use the optional argument end to explicitly mention the string that should be appended at the end of the line. Whatever you provide as the end argument is going to be the terminating string. So if you provide an empty string, then no newline characters, and no spaces will be appended to your input. Easy, right?

print("First Line ", end = '')
print("Second Line")
# output:
# First Line Second Line

Python 2.x

In Python 2.x, add a comma at the end of your print statement to make sure print() doesn’t print a new line.

print("First Line ", end = '')
print("Second Line")
# output:
# First Line Second Line

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.