Python: How to Print without Newline or Space

Python: How to Print without Newline or Space

Introduction

The print() function in Python appends a newline to the output when displayed on the tty (teletypewriter A.K.A the terminal). When you don't want your message displayed with newlines or with spaces, how can you change the behavior of print()?

This can easily be achieved by altering the default values of the sep and end parameters of the print() function.

Printing Without a Newline

Until Python version 2.x, print was a reserved keyword that acts as a special statement. Since Python version 3.x, the print command has evolved into a function.

This version of print() is capable of taking the following arguments:

The values (value1, value2) mentioned above can be any string or any of the data types like list, float, string, etc. The other arguments include a separator (sep) used to divide the values given as arguments whereas the argument end is the \n newline character by default. This is the reason why whenever the print() function is called, the cursor slides to the next line.

In Python 3.x, the most straightforward way to print without a newline is to set the end argument as an empty string i.e. ''. For example, try executing the following snippet in your Python interpreter:

print("I am a sentence", "I am also a sentence")

The interpreter would output the following:

I am a sentence I am also a sentence
>>>

We are printing two strings, so Python will use the value of sep, a blank space by default, to print them together. Python also adds a newline character at the end, so the interpreter prompt goes to the end line.

Now modify the previous statement to look like this:

print("I am a sentence", "I am also a sentence", sep="; ", end="")

Upon executing it in the interpreter, you will get an output resembling:

I am a sentence; I am also a sentence>>>

Two things happened here - the separator between the two strings now also includes a semicolon. The interpreter prompt also appears on the same line because we removed the automatically appended newline character.

Printing Without a Newline in Python 2.X

For earlier versions of Python - less than 3 but greater than 2.6 - you can import print_function from the __future__ module. This will override the existing print keyword with the print() function as shown below:

from __future__ import print_function

print("I am a sentence", "I am also a sentence", sep="; ", end="")

This will also yield:

Free eBook: Git Essentials

Check out our hands-on, practical guide to learning Git, with best-practices, industry-accepted standards, and included cheat sheet. Stop Googling Git commands and actually learn it!

I am a sentence; I am also a sentence>>>

This is how you can use Python version 3's print() function in Python 2.x.

Using stdout.write()

The sys module has built-in functions to write directly to the file or the tty. This function is available for Python 2.x and 3.x versions. We can use the write() method of the sys module's stdout object to print on the console like this:

import sys

sys.stdout.write("I am a line")

Let's execute this and take a look at the output:

I am a line>>>

Although this gives the output of what we are trying to achieve, there are quite a few differences between the write() function and the print() function. The print() function can print multiple values at a time, can accept non-string values, and is friendlier to developers.

Conclusion

In this article, we have explored the different ways by which values can be printed without a newline character/carriage return. This strategy can come quite handy while printing the elements in the outputs of algorithms such as a binary tree or printing the contents of a list next to each other.

Last Updated: August 25th, 2021
Was this article helpful?

Improve your dev skills!

Get tutorials, guides, and dev jobs in your inbox.

No spam ever. Unsubscribe at any time. Read our Privacy Policy.

Sathiya Sarathi GunasekaranAuthor

Pythonist 🐍| Linux Geek who codes on WSL | Data & Cloud Fanatic | Blogging Advocate |
Author

Want a remote job?

    Prepping for an interview?

    • Improve your skills by solving one coding problem every day
    • Get the solutions the next morning via email
    • Practice on actual problems asked by top companies, like:
     
     
     

    Better understand your data with visualizations

    With over 330+ pages, you'll learn the ins and outs of visualizing data in Python with popular libraries like Matplotlib, Seaborn, Bokeh, and more.

    © 2013-2021 Stack Abuse. All rights reserved.