How to print in one line in Python

In this post, we are going to learn how to How to print in one line in Python. by default print() statement print output in a newline.But In python3+ extra argument end=”” is introduced for print() function to print output without Newline.Instead of printing without a newline, we can add a special character or string with the help of the end=” parameter. To print a number without a newline or space, we have to pass end=” at end of the print() statement with or without space

1. Python2.X print without Newline


To print without newline in Python2.x We use comma(,) at end of the different print statements. Let us understand with the below example.

print "Hi there! Welcome", 
print "to devenum.com",
print "Thanks For Visiting"

Output

Hi there! Welcome to devenum.com Thanks for Visting

2. Python3.X print in one-line


In Python3.x print() function extra end parameter is introduced that helps to print without a newline. Instead of printing without a newline, we can add a special character or string with the help of the end=”” parameter. Let us understand with example. The end=”” parameter helped us print without a new line, but there is no space between the output strings. We can add special character space ” ” to get space between different string output.

print("Good Moring", end="")
print("Hope we are doing well!")

#add space 
print()
print("Good Moring", end=" ")
print("Hope we are doing well!")
print()

print("Hi! admin", end ="@devnum.com")

Output

Good MoringHope we are doing 
Good Moring Hope we are doing well!
Hi!,admin@devenum.com

3. How to print in one line in Python using Sys module


The Python built-in sys module stdout.write() method is used to print python string without a newline. To use this module first we have to import in out program and use it stdout.write() method to print output in one line.Let us understand with the below example

import sys

sys.stdout.write("Hi there!")
sys.stdout.write("Welcome to devenum.com.Thanks for visting US")

Output

Hi there!Welcome to devenum.Thanks for visting US

5. How to print numbers in one line without space Python


In this example we are printing number 1 to 10 in one line without space by using the end parameter of print statement.

num = int(input("Please Enter the value of n:")) 
for i in range(num): 
  print(i, end="") 

Output

Please Enter the value of n:10
0123456789

6. How to Print a list without a newline in Python


Sometimes we have to print all elements of a list in one line in python. We have already understood the end=”” parameter in print statement. Now let us use it to print python list.

list = ["C#", 3, "Java", 6, 9,12,17]
print('list elements :')
for item in list:
	print(item, end=" ")

Output

list elements :
C# 3 Java 6 9 12 17 

Summary

In this post, we have learned How to print in one line in Python with examples in python 3.x and python2.x.