Python: How to Print Without Newline? (The Idiomatic Way)

Jump straight to the solution:

Printing without a newline in Python3

Printing without a newline in Python2

The Problem

Python is one of the easiest programming languages to learn.

One of the first programs that you write when you start learning any new programming language is a hello world program.

hello world program in python looks like this

# hello world in python
print("Hello World!")

It is that easy!

Just one line and boom you have your hello world program.

In Python 3, print() is a function that prints out things onto the screen (print was a statement in Python 2).

As you can see, it is a very simple function.

Yet there is one thing that is really annoying about this function.

It automatically prints a newline ‘\n’ at the end of the line!

Let’s take a look at this example

print("Hello World!")
print("My name is Karim")
# output:
# Hello World!
# My name is Karim

As you can notice, the two strings are not printed one after the other on the same line but on separate lines instead.

Even though this might be what you actually want, but that’s not always the case.

if you’re coming from another language, you might be more comfortable with explicitly mentioning whether a newline should be printed out or not.

For example in Java, you have to explicitly indicate your desire to print a newline by either using the println function or typing the newline character (\n) inside your print function:

// option 1
System.out.println("Hello World!")
// option 2
System.out.print("Hello World!\n")

So what should we do if we want no newline characters in python?

Let’s jump right into the solution!

The Solution

Printing with no newlines in Python 3

Python 3 provides the simplest solution, all you have to do is to provide one extra argument to the print function.

# use the named argument "end" to explicitly specify the end of line string
print("Hello World!", end = '')
print("My name is Karim")
# output:
# Hello World!My name is Karim

You can use the optional named 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.

Printing with no newlines in Python 2

In python 2, the easiest way to avoid the terminating newline is to use a comma at the end of your print statement

# no newlines but a space will be printed out
print "Hello World!",
print "My name is Karim"
# output
# Hello World! My name is Karim

As you can see, even though there was no newlines, we still got a space character between the two print statements.

If you actually needed a space, then this is the simplest and most straightforward way to go.

But what if you want to print without a space or newline?

In this you can, you should use the all-powerful sys.stdout.write function from the sys module.

This function will only print whatever you explicitly tell it to print.

There are no terminating strings.

There is no magic!

Let’s take an example

import sys

sys.stdout.write("Hello World!")
sys.stdout.write("My name is Karim")
# output
# Hello World!My name is Karim

Conclusion

In Python 3, you can use the named end argument in the print function and assign an empty string to this argument to prevent the terminating newline.

In Python 2, you can either use a comma after your print statement if you don’t mind the space, or you can just use the sys.stdout.write() function

Learning Python?

Check out the Courses section!

Featured Posts

Are you Beginning your Programming Career?

I provide my best content for beginners in the newsletter.

  • Python tips for beginners, intermediate, and advanced levels.
  • CS Career tips and advice.
  • Special discounts on my premium courses when they launch.

And so much more…

Subscribe now. It’s Free.

Subscribe
Notify of
4 Comments
Oldest
Newest
Inline Feedbacks
View all comments
6 years ago

Awesome …

Bob Evans
5 years ago

Thank you, Karim. This feature of Python really irritated me… At a restaurant, perhaps I like getting more (good things) than I had asked for, but in programming, I want exactly and only what I asked for. With your help, that’s what Python is now giving me!

Helen
5 years ago

Thanks!