Python New Line: How to Print WITHOUT Newline in Python

โšก Smart Summary

Python print() adds a newline by default, but you can avoid it. Here you will learn the end parameter, the Python 2 comma, sys.stdout.write(), and loops to keep output on one line.

  • ๐Ÿšซ No newline: Set end to an empty string so output stays on one line.
  • โž• Custom end: end can hold a space, string, or symbol between items.
  • ๐Ÿ Python 2: In Python 2.x, add a trailing comma to skip the newline.
  • ๐Ÿ–ฅ๏ธ sys module: sys.stdout.write() prints text without adding a newline.
  • ๐Ÿ“‹ Print lists: Loop a list with end set to a space to join items.
  • ๐Ÿค– AI help: AI tools like Copilot suggest end and print patterns instantly.

Python New Line Print Without Newline

Python print() built-in function is used to print the given content inside the command prompt. The default functionality of Python print is that it adds a newline character at the end.

Working of Normal print() function

The print() function is used to display the content in the command prompt or console.

Here is an example that shows the working of Python print without newline function.

print("Hello World")
print("Welcome to Guru99 Tutorials")

Output:

Hello World
Welcome to Guru99 Tutorials

In the given output, you can see that the strings that we have given in print() are displayed on separate lines. The string “Hello World ” is printed first, and “Welcome to Guru99 Tutorials” is printed on the next line.

How to print without a newline in Python?

From Python 3+, there is an additional parameter introduced for print() called end=. This parameter takes care of removing the newline that is added by default in print().

In the Python 3 print without newline example below, we want the strings to print on same line in Python. To get that working just add end=”” inside print() as shown in the example below:

print("Hello World ", end="")
print("Welcome to Guru99 Tutorials")

Output:

Hello World Welcome to Guru99 Tutorials

We are getting the output that we wanted, but there is no space between the strings. The string Hello World and Welcome to Guru99 Tutorials are printed together without any space.

In order to add space or special character or even string to be printed, the same can be given to the end=”” argument, as shown in the example below.

print("Hello World ", end=" ")
print("Welcome to Guru99 Tutorials")

So I here we have added one space to the end argument, for example (end=” “). Now, if you see the output, you should see a space between Hello World and Welcome to Guru99 Tutorials.

Output:

Hello World  Welcome to Guru99 Tutorials

It’s not only space that you can give to the end argument, but you can also specify a string you wish to print between the given strings. So here is an example of it

print("Hello World ", end="It's a nice day! ")
print("Welcome to Guru99 Tutorials")

Output:

Hello, World It's a nice day!  Welcome to Guru99 Tutorials

Print Without Newline in Python 2.x

To get the strings to print without a newline, in python2.x you will have to add a comma (,) at the end of the print statement as shown below:

print "Hello World ", 
print "Welcome to Guru99 Tutorials."

Output:

Hello World  Welcome to Guru99 Tutorials

Using Python sys module

Yet another method that you can use to print without newline in Python is the built-in module called sys.

Here is a working example that shows how to make use of the sys module to print without newline Python strings.

To work with the sys module, first, import the module sys using the import keyword. Next, make use of the stdout.write() method available inside the sys module, to print your strings.

import sys

sys.stdout.write("Hello World ")
sys.stdout.write("Welcome to Guru99 Tutorials")

Output:

Hello World Welcome to Guru99 Tutorials

Using print() to print a list without a newline

Consider a list of items for example: mylist = [“PHP”, JAVA”, “C++”, “C”, “PHYTHON”] and you want to print the values inside the list using for-loop. So here you can make use of print() to display the values inside the list as shown in the example below:

mylist = ["PHP", "JAVA", "C++", "C", "PHYTHON"]
for i in mylist:
	print(i)

Output:

PHP
JAVA
C++
C
PHYTHON

The output shows the list items, each printed one after another, on a new line. What if you want all the items on the list in the same line? For that, make use of end argument inside print() that will remove new line in Python and print all items of the list in the same line.

mylist = ["PHP", "JAVA", "C++", "C", "PYTHON"]
for i in mylist:
	print(i, end=" ")

Output:

PHP JAVA C++ C PHYTHON

Printing the star(*) pattern without newline and space

The example of Python print without newline will make use of print() function to print stars(*) on the same line using for-loop.

for i in range(0, 20):
    print('*', end="")

Output:

********************

FAQs

In Python 3, pass end=” to print(), like print(text, end=”). This stops the default newline.

Set end to a space: print(text, end=’ ‘). Any string given to end separates items instead of a line break.

In Python 2.x, add a trailing comma after print, like print ‘text’,. The comma suppresses the newline.

Yes. sys.stdout.write(text) prints without a newline. Import sys first; unlike print(), it never adds a line break.

Loop the list and print each item with end set to a space: for i in mylist: print(i, end=’ ‘).

AI assistants like GitHub Copilot autocomplete print() with the right end parameter and suggest sys.stdout.write() as you type.

Yes. AI-powered tools add the end parameter, convert Python 2 prints to Python 3, and refactor output loops automatically.

print() appends a newline so each call starts fresh. The end parameter overrides this default.

Summarize this post with: