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?

If you are a beginner, then I highly recommend this book.

No longer a beginner?

Then it’s time to take your Python skills to the next level with this book (It’s my favorite).

Featured Posts

Python: A Learning Path from Zero to Hero

The Ultimate Path for Learning Computer Science

Pass your Coding Interview like a Boss

A Roadmap for Learning Git

Why (and How) you should Start your Programming Blog Today?

Are you Beginning your Programming Career?

I provide my best content for beginners in the newsletter.

  • What programming language to start with?
  • Do you need a CS degree to be a programmer?
  • Career tips and advice
  • Programming tutorials

And so much more…

Subscribe now. It’s Free.

2019-01-13T03:33:02+00:00 By |

3 Comments

  1. Ashwani Kumar September 13, 2018 at 1:21 am - Reply

    Awesome …

  2. Bob Evans April 4, 2019 at 1:17 pm - Reply

    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!

    • Karim April 6, 2019 at 8:46 pm - Reply

      Hi Bob! I totally feel you :). I came to Python from C/C++ so I don’t feel very comfortable getting more than what I ask for as well

Leave A Comment