Python Print without Newline

In this post, we will see how to print without Newline in Python.

In python, when you use two print statements consecutively, it will print in different line.

print('Hello world')
print('from Java2blog')

Output:

Hello world
from Java2blog

As expected, we got Hello world and from Java2blog

If you want to print these two print statements in the same line, you can do it in different ways.
[toc]

💡 Did you know?

If you are using python 2, then you can simply put comma(,) to print statements in same line.

#print without new line but with space
print('Hello world'),
print('from Java2blog')

Using print statement

You can use end='$delimiter' with print statements and it will print string with delimiter.

Print with space

You can use end=' ' for printing statements separated by space.

print('Hello world',end=' ')
print('from Java2blog')

Output:

Hello world from Java2blog

Print with comma(,)

You can use end=',' for printing statements separated by comma.

print('Hello world',end=',')
print('from Java2blog')

Output:

Hello world,from Java2blog

Using sys library

You can also import sys library and use sys.stdout.write() to print on same line.

import sys
sys.stdout.write('Hello world')
sys.stdout.write(' from Java2blog')

Output:

Hello worldfrom Java2blog

Printing without newline is very useful while printing using a loop. If you don’t want to print each element in an individual line, you can use end=' ' with print statement to print on the same line.

countryList =["India","China","Russia","France"]
for country in countryList:
    print(country,end=' ')

Output:

India China Russia France

As you can see, all the elements are printed on same line separated by space(' ')

That’s all about Python Print without Newline.

[social_share share_on="facebook,twitter,linkedin,whatsapp"][post_navigator in_same_term="true"]

Leave a Reply

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

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.