Python, How To Print Without Newline or Space?

RSS
EMAIL
FACEBOOK
FACEBOOK
GOOGLE
GOOGLE
https://www.poftut.com/python-how-to-print-without-newline-or-space/
PINTEREST
PINTEREST
INSTAGRAM

I have a simple problem with python about new line while printing. How can I print without newline or space? In python print will add at the end of the given string data \n newline or a space. But in this situation we do not will use append string to stdout .

Python2

We generally prefer Python 3 in our posts but there are some people who uses python2 too so in this situation we will provide python2 way solution. We will import sys and use stdout.write functions and flush given data to the stdout without a newline.

Another way is if we are using python2.6 and above we can use python3 solution like below.

Python3

Python 3 provides simple and convenient solution for this issue. print function provides more than string to be printed. print function accepts more parameters like end . end parameter is used to specify the line end character. We will set end option to nothing and this will remove default \n or end of line or space.

Change separator

Another way to remove spaces in multiple print argument is using sep option of the print function. We can specify any character as separator. In this example we use empty string as separator which will join two string together.

LEARN MORE  C - File Operations, Open, Write and Close Files

Change separator

Change separator

RSS
EMAIL
FACEBOOK
FACEBOOK
GOOGLE
GOOGLE
https://www.poftut.com/python-how-to-print-without-newline-or-space/
PINTEREST
PINTEREST
INSTAGRAM

You may also like...

Leave a Reply

Your email address will not be published.

Enjoy this blog? Please spread the word :)