What’s New ?

The Top 10 favtutor Features You Might Have Overlooked

Read More

Python: How to Print without newline (3 Methods)

  • Sep 11, 2024
  • 6 Minute Read
  • Why Trust Us
    We uphold a strict editorial policy that emphasizes factual accuracy, relevance, and impartiality. Our content is crafted by top technical writers with deep knowledge in the fields of computer science and data science, ensuring each piece is meticulously reviewed by a team of seasoned editors to guarantee compliance with the highest standards in educational content creation and publishing.
  • By Komal Gupta
Python: How to Print without newline (3 Methods)

In any programming language, displaying output to the console is a basic operation. Depending on the situation, you may need to customize your print output, so today In this article, we'll look at a solution to a Python problem that involves printing output without a newline.

While printing output is a simple task thanks to Python's print() function, there are situations where you may want to print multiple outputs on the same line without introducing a new line in between. 

As we all know, in Python a newline character (\n) is automatically added at the end of each output by the Python print() method to avoid this we have some methods below.

Method 1: Using Sys library Python makes it easy to print without a newline

To write output to the terminal without a newline character, we can substitute the sys.stdout.write() function for the print() function.

import sys

sys.stdout.write("Hello")
sys.stdout.write(" Welcome to Favtutor")

Output:

Hello Welcome to Favtutor
20

Note: This function also returns the character length of the last string passed above 20, which is the length of the string " Welcome to Favtutor".

Method 2: Using the End Parameter

The print() function's end parameter in Python 3 can be used to get rid of the newline character. The end parameter's default value is the newline character, or '\n'. To print without a newline character, we can set it to either the empty string or any other character/string.

print("Hello, World!", end='')
print(" Welcome to ",end='-')
print("Favtutor")

Output:

Hello, World! Welcome to -Favtutor

Method 3: Using the sep Parameter

To define a different separator between the items to be printed, use the print() function's sep parameter. Instead of using the standard space character to divide the items, this argument specifies a string. This is a useful method when you want to combine two print statements in one line instead of writing 2 lines of code for that.

The strings "Hello" and "world" are joined in the code below and are separated by a space, or any other string that is, proving that "sep" works between strings. When the 'end' option is used, however, the string's end is affected. 

1. Using Sep Parameter

print('Hello','World' ,sep=' ')
print('Hello')
print('World')

Output:

Hello World
Hello
World

2. Using End Parameter

print('Hello','World' ,end=' ')
print('Hello')
print('World')

Output:

Hello World Hello
World

In Older Version like Python 2.x you can find below method also working but this does not work in Python 3

Conclusion

In Python3, printing without a newline character is necessary for output formatting. In this post, we learnt how to print text in Python without a newline by using the sys.stdout.write() method and the print() function's end option.To get our text to appear on the same line in Python 2.x, we may add a comma to the end of our print statement.

Happy Learning :)

FavTutor - 24x7 Live Coding Help from Expert Tutors!

About The Author
Komal Gupta
I am a driven and ambitious individual who is passionate about programming and technology. I have experience in Java, Python, and machine learning, and I am constantly seeking to improve and expand my knowledge in these areas. I am an AI/ML researcher. I enjoy sharing my technical knowledge as a content writer to help the community.