Register Login

Python: How to Print Without Newline?

Updated Jan 08, 2024

Unlike other programming language, users, while printing multiple statements in Python, returns them in new lines. In other words, Python, by default, prints these statements with a newline character, and users require separate arguments or methods to avoid this newline character.

These are sys.stdout.write(), str.format() method, sep = "", end = "", etc., for controlling the output to generate a more visually endearing display. This article will provide a detailed explanation of printing without newline in Python using the above-cited techniques.

Let us see what Python prints by default with a newline character.

Users switching from C/C++ often try to print multiple statements or objects without a newline in Python. However, since the Python print() function returns statements ending with a newline character, it will not work in our case. For example, if we print in C using the printf() function with two separate statements, it returns both statements in a single line.

Code Snippet:

#include <stdio.h>

int main()
{
    printf("This is C ");
    printf("Let's study newline");

    return 0;
}

Output:

But when we print the same statements in Python, the print() function returns in separate lines.

Code Snippet:

print("This is C ");
print("Let's study newline");

Output:

It is what we want to ignore. Let us use some methods to print the statements in the same line rather than going to the subsequent line.

Method 1: Using the end parameter

The end parameter adds another statement or string at the end of the output. By default, since the print() function ends with a newline character, this parameter specifies an end character with whitespace to avoid a newline.

Code Snippet:

print("This is the first statement", end="")
print(" and this is the second statement")

Output:

Explanation:

The first print() function will print the first statement. Then, we used the end parameter, which indicates a whitespace, and finally, we printed the second statement with the last print() function.

Method 2: Using sys.stdout.write() method

Users can also use the sys.stdout.write() method that prints a string input directly in the output console. It does not append a newline specifier. Users should also call sys.stdout.flush() to confirm the compiler should display the output immediately.

Code Snippet:

import sys
sys.stdout.write("This is an example ")
sys.stdout.flush()
sys.stdout.write("of Python!")
sys.stdout.flush()

Output:

Explanation:

The sys module has different functions and methods, among which the stdout.write() method prints multiple statements without including newline characters. But this technique is more lengthy than simply using the prin() function with the end parameter with whitespace character to avoid newline.

Method 3: Using the sep parameter

The sep parameter, also called a separator specifies the separation between multiple statements inside the print() function. This argument inserts space between the statements passed in the Python print() function by default.

Code Snippet:

print("This ", "is ", "Python. ", sep="")

Output:

Explanation:

In the above example, we used the sep parameter, which formats the output strings. By default this parameter does not separate the statements with space, so we used whitespace after each word in the print() function to maintain a gap between the words.

Method 4: Using both sep and end parameters

Users can also use the sep and end parameters to control the output from the print() function completely.

Code Snippet:

print("This is ", sep = ":", end = "Python.")
print(" Also, this is another print statement", sep = " ")

Output:

Explanation:

Using the sep and end parameters, users can format the string in any order they specify within the parameters. This technique lets users control the string output with perfect positioning and formatting.

Method 5: Using print() function inside for loop

Let us create a statement using the for loop and print() function.

Code Snippet:

demo = ["Let", "us", "create", "a", "new", "statement."]
for i in demo:
print(i, end = " ")

Output:

Method 6: Combining multiple strings using the plus operator

Preventing the newline characters also includes this technique where users can use the string concatenation as a medium to print multiple statements without any newline characters.

Code Snippet:

print("Hello " + "Python")

Output:

Explanation:

This technique is easy when users put simple strings inside the print() function. But also, it may become clunky for more complex strings with or without whitespace or multiple statements.

Method 7: Using F-strings

F-strings render a more concise and convenient way to print without newline characters in Python since it has embedded Python expressions inside string literals to format. Further, users can combine string values with numbers and other variable values by directly inserting the expressions within string literals.

Code Snippet:

name = "Hello"
a = 0
b = 10
print(f"{name}, can I make the range from {a} to {b}.")

Output:

Method 8: Using the str.format() method

It is an alternative technique to format strings in the output console. It is a powerful tool in Python, allowing users to print formatted strings by embedding variables and values into the placeholder braces {}.

Code Snippet:

a = "Hi"
b = 0
c = 10
print("{0}, can we specify the range from {1} to {2}.".format(a, b, c))

Output:

Python Print Without Newline Video Tutorial

Conclusion:

Finally, we conclude that we have grasped a deep-dyed understanding of printing without newlines in Python. However, string concatenation and formatting both provide flexibility compared to other techniques like end parameters, for loop, etc.

This article covered printing two or more lines by replacing the default newline character in the print() function. All these techniques can fine-tune the output and print it in the desired format.


×