Home DevOps Python Print Without Newline

Python Print Without Newline

by admin

How to use the Python print function to print without a newline. Coming from working in another language, it’s a common question to wonder how to print two or more variables or strings on the same line using Python. Note that how the print function works in Python3 is different to how it works in Python2, so we will take a look at both in this article. If you’re only interested in one or the other, you can skip straight to the relevant sections using these links:

How to print with no newline using Python 3

By default, the print() function in Python defaults to ending with a new line. For example, if you were to run:

print("My 1st String"); print ("My 2nd String") 

The output would be:

My 1st String
My 2nd String

But what if we want the output to look like this:

My 1st String, My 2nd String

In Python 3.x we can use the ‘end=‘ parameter in the print function. By default the end parameter uses ‘\n’ which is what creates the newline, but we can set a different value for the end parameter. This tells it to end the string with a character of our choosing rather than ending with a newline. For example:

print("My 1st String", end=","); print ("My 2nd String.")

This results in:

My 1st String, My 2nd String.

python print without newline

You can also use this in a loop. For example, if we wanted to count to 10, but place ever number on the same line, separated with a ‘.’ we could use:

# python print on same line without space
for i in range(1,10):
    print(i, end='.')

This results in:

1.2.3.4.5.6.7.8.9.

If you wanted a space between the numbers rather than a ‘.’ then you can change it to:

# python print on same line without space
for i in range(1,10):
    print(i, end=' ')

This results in:

1 2 3 4 5 6 7 8 9

And now with Python 2.7

Python 2.x works differently in that there is no ‘end’ option. Instead, in order to place the output of two print statements onto the same line using Python 2 and then you simply add a comma after the first statement. For example:

print "My 1st String,",
print "My 2nd String"

This results in:

My 1st String, My 2nd String

Printing a Newline in Python

This is kind of the opposite of the purpose of this article, but I get a few search hits looking for how to print a newline in python, so thought I would also address that here. As shown in earlier examples, Python defaults to printing separate print statements on new lines, but what if you want to do this with a single print statement? The answer is to use \n in your print statement. For example:

print("My 1st Statement \nMy 2nd Statement")

This results in:

print("My 1st Statement \nMy 2nd Statement")
My 1st Statement
My 2nd Statement

Another way to do this would be to use the sep parameter. This allows us to set the value of the separator character when printing multiple strings with the same print statement. For example, the put a comma between two strings we could use:

print("My 1st Statement", "My 2nd Statement", sep=",")
My 1st Statement,My 2nd Statement

Now, to put a new line between each string we can use the newline character as the value of the sep parameter:

print("My 1st Statement", "My 2nd Statement", sep="\n")
My 1st Statement
My 2nd Statement

Now you know how to print in Python without newline! And also how to print to a newline! Happy coding! Whilst you’re here, if you’re interested in learning about multiline comments in Python, check out this post.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More