How to Print without newline in Python?
Last Updated :
26 Apr, 2025
In Python, the print() function adds a newline by default after each output. To print without a newline, you can use the end parameter in the print() function and set it to an empty string or a space, depending on your needs. This allows you to print on the same line. Example:
Python
print("geeks", end =" ")
print("geeksforgeeks")
Outputgeeks geeksforgeeks
Explanation: In the first print() statement, end=" " is used, which means a space (" ") is printed instead of the default newline after the word "geeks". The second print() statement prints "geeksforgeeks" immediately after "geeks", on the same line.
Using Python join
You can use the join() method to combine elements of an iterable into a single string and then print it.
python
a = ["Hello", "World"]
print(" ".join(a))
Explanation: " ".join(a) Joins the elements of the list a using a space as the separator, resulting in the string "Hello World".
Using asterisk ( * ) operator
In Python, you can use the asterisk (*) operator to unpack a list or tuple and print its elements without adding a newline between them.
Python
a = [1, 2, 3, 4, 5, 6]
print(*a)
Explanation: The * operator unpacks the list, passing its individual elements as separate arguments to the print() function. This prints the list elements separated by spaces.
Using Python sys Module
To use the sys module, first, import the module sys using the import keyword. Then, make use of the stdout.write() method available inside the sys module, to print your strings. It only works with string If you pass a number or a list, you will get a TypeError.
Python
import sys
sys.stdout.write("GeeksforGeeks ")
sys.stdout.write("is best website for coding!")
OutputGeeksforGeeks is best website for coding!
Explanation: Unlike the print() function, which automatically adds a newline at the end of the output, sys.stdout.write() writes the specified text to the console exactly as it is, without any extra characters (like a newline).
Related Articles:
How to Print without Newline in Python?
Similar Reads
Read a file without newlines in Python When working with files in Python, it's common to encounter scenarios where you need to read the file content without including newline characters. Newlines can sometimes interfere with the processing or formatting of the data. In this article, we'll explore different approaches to reading a file wi
2 min read
How to print spaces in Python3? In this article, we will learn about how to print space or multiple spaces in the Python programming language. Spacing in Python language is quite simple than other programming language. In C languages, to print 10 spaces a loop is used while in python no loop is used to print number of spaces. Foll
2 min read
How to Print String and Int in the Same Line in Python Printing both texts and integers on the same line is a common chore for programmers. Python, fortunately, has various methods for accomplishing this, making it simple to display a combination of text and numerical values in your output. In this article, we'll explore various ways to print strings an
2 min read
Writing to file in Python Writing to a file in Python means saving data generated by your program into a file on your system. This article will cover the how to write to files in Python in detail.Creating a FileCreating a file is the first step before writing data to it. In Python, we can create a file using the following th
4 min read
Print lists in Python Printing a list in Python is a common task when we need to visualize the items in the list. There are several methods to achieve this and each is suitable for different situations. In this article we explore these methods.The simplest way of printing a list is directly with the print() function:Pyth
3 min read
How to Split a File into a List in Python In this article, we are going to see how to Split a File into a List in Python. When we want each line of the file to be listed at consecutive positions where each line becomes an element in the file, the splitlines() or rstrip() method is used to split a file into a list. Let's see a few examples
5 min read