How to print on same line with print in Python

In Python, when you use the print function, it prints a new line at the end.

For example:

print "This is some line."
print "This is another line."

Output:

This is some line.
This is another line.

What if you want to avoid the newline and want to print both statements on same line? Well, there are 2 possible solutions.

Add comma at the end of print

print "This is some line.",
print "This is another line."

Output:

This is some line. This is another line.

If you are using Python 3 then use the below:

print ("This is some line.", end="")
print ("This is another line.")

Use sys module

import sys
sys.stdout.write("This is some line.")
sys.stdout.write("This is another line.")

Help us improve this content by editing this page on GitHub