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

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

 

The Problem
Python is one in all the best programming languages to learn.

One of the primary methods which you simply write while you begin studying any new programming language is a howdy international program python print without newline.

A howdy international application in python seems to be like this

# howdy international in python
print(“Hello World!”)
It is that easy!

Just one line and growth you’ve got your howdy international program.

In Python 3, print() is a role that prints out issues onto the display (print was a statement in Python 2).

As you possibly can see, it’s a in actual fact easy function.

Yet there’s one factor that’s in actual fact annoying about this function.

It automatically prints a newline ‘\n’ on the cease of the line!

Let’s take a appear at this example

print(“Hello World!”)
print(“My identify is Karim”)
# output:
# Hello World!
# My identify is Karim

As you possibly can notice, the NULL strings usually aren’t revealed one after the different on the similar line but on separate strains instead.

Even although this may be what you in actual fact want, but that’s now not all the time the case.

if you’re coming from one other language, you may be additional soft with explicitly mentioning regardless of even if a newline have got to be revealed out or not.

For instance in Java, you’ve got got to explicitly suggest your hope to print a newline via both utilizing the println role or typing the newline individual (\n) inner your print function:

// choice 1
System.out.println(“Hello World!”)
// choice 2
System.out.print(“Hello World!\n”)

So what have got to we do if we hope no newline characters in python?

Let’s leap proper into the solution!

The Solution
Printing with no newlines in Python 3
Python 3 adds the best solution, all you’ve got got to do is to supply one additional argument to the print function.

# use the named argument “end” to explicitly specify the cease of line string
print(“Hello World!”, cease = ”)
print(“My identify is Karim”)
# output:
# Hello World!My identify is Karim

You can use the optionally available named argument cease to explicitly point out the string that have got to be appended on the cease of the line.

Whatever you supply because the cease argument goes to be the terminating string.

So in the event you happen to supply an empty string, then no newline characters, and no areas will likely be appended on your input.

Printing with no newlines in Python 2
In python 2, the best strategy to keep away from the terminating newline is to make use of a comma on the cease of your print statement

# no newlines but a area will likely be revealed out
print “Hello World!”,
print “My identify is Karim”
# output
# Hello World! My identify is Karim

As you possibly can see, even although there was no newlines, we nonetheless got a area individual among the NULL print statements.

If you in actual fact wanted a space, then here is the best and most truthful strategy to go.

But what in the event you happen to hope to print with out a area or newline?

In this you can, you’ve got got to make use of the all-powerful sys.stdout.write role from the sys module.

This role will merely print no matter you explicitly inform it to print.

There aren’t any terminating strings.

There is now not any magic!

Let’s take an example

import sys

sys.stdout.write(“Hello World!”)
sys.stdout.write(“My identify is Karim”)
# output
# Hello World!My identify is Karim

Conclusion
In Python 3, you possibly can use the named cease argument within the print role and assign an empty string to this argument to hinder the terminating newline println python.

In Python 2, you possibly can both use a comma after your print statement in the event you happen to don’t thoughts the space, otherwise you possibly can simply use the sys.stdout.write() function print new line python

Leave a Reply

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