Print Without Newline in Python (Print in the same line)

Print Without Newline In Python (Print In The Same Line)

Learn to use print multiple items in the same line on the console screen in Python. Learn to prevent print statements to print in new line each time.

When you use two or more print statements in Python, each time, the contents are printed in a new line as demonstrated by following code and its console output.

print("WTMatter")
print("Gurmeet Singh")

Output.

WTMatter
Gurmeet Singh

But now the issue is what if we do not want to print the contents of the second print statement in another line? That’s all we’re going to figure out in this tutorial.

Ways To Print in the same line (Without New Line Breaks)

There are multiple ways in Python using which you can print multiple contents without new line breaks, in the same line using either a single or multiple print statements. Two methods are illustrated below with the help of examples.

Method 1 (Different for Version 2.X and 3.X)

This method is different for version 2.X and 3.X. In Version 2.X, all you need do is to use a comma after the print statement to avoid the line break. This will print the contents that the statement without the new line. If you use multiple print statements and separate them with commas, then the contents will although be printed in the same line yet will be separated with whitespace.

For the same purpose in versions 3.X, you need to specify the end character of the line using the end argument passed to the print statement.

Version 2.X – Using Comma After Print Statement

In the following code, we’re first printing three different objects using three different statements in the same line. Also, we’ve used a for loop to print the items of a Python List but all in the same line using the same technique by ending the print statement with a comma.

Output.

WTMatter Gurmeet Singh 21
A B C 1 2 3

Version 3.X – Using the end Argument

The contents that we’re printing using the following code remains the same. The difference here is that instead of using commas between the multiple print statements, here, we’re passing the end argument and specifying a whitespace string value to it.

The end argument is basically used to specify the character to print after the contents that are supposed to printed using this statement. The default value of this argument is a new line character \n, that’s why by default the print statements are printed with new lines.

#Multiple Print Statements
#With end argument passed as whitespace string

print("WTMatter", end = " ")
print("Gurmeet Singh", end = " ")
print(21)

#A Python List
items = ["A", "B", "C", 1, 2, 3]

#Looping through items List
for item in items:
    print(item, end = " ")

Output.

WTMatter Gurmeet Singh 21
A B C 1 2 3

Method 2

Another way that works for versions 2.x and 3.x is to simply use a single print statement and operating the contents using a comma within the statement itself. The print statement can take multiple objects to be printed as its arguments. The following code illustrates the same.

#Using Single Print Statement
#With Multiple Items to print
#Seprated with commas

print("WTMatter", "Gurmeet Singh", 21)

#A Python List
items = ["A", "B", "C", 1, 2, 3]

print(items[0], items[1], items[2], items[3], items[4], items[5])

(Obviously, it’s not a good way to print the items of a list like this)

Output.

WTMatter Gurmeet Singh 21
A B C 1 2 3

Related Article.

I hope you found this guide useful. If so, do share it with others who are willing to learn Python. If you have any questions related to this article, feel free to ask us in the comments section.

And do not forget to subscribe to WTMatter!

Subscribe To WTMatter!

Receive updates of our latest articles via email. Enter your email address below to get started.

Leave a Reply

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