Python print without newline
In this post, you will learn how to print statements in Python without using a newline. There are different ways to print without a newline. Here, we have mentioned some of them.
The new line character in Python is utilized to check the end of a line and the start of another line. Realizing how to utilize it is fundamental in the event that you need to print output to the console and work with documents. As we know, the new line character is denoted as \n. When we use this character in a string, it implies that the current line is closed by then and another line begins just after it. Most likely in every programming language, we probably started our journey by learning about the print() statement. By default, the print statements add a new line character at the end of the string.
print("Hello John!")
print("How are you?")
Output of the above code-
Hello John!
How are you?
As you can see in the above example, all the statements are printed on a separate newline. What if we want both of these strings to appear on the same line?
Python print without newline in Python 3.x
In Python 3+, the print() statement comes up with many optional arguments. The end argument helps us by removing the newline. It specifies what to print at the end. The default value is '\n'.
Syntax-print(object(s), end=end)
So, if there is a need to print the strings on the same line in Python, we just need to add end="" inside the print() statement. The following example demonstrates this-
print("Hello John! ", end="")
print("What is going on?")
Output of the above code -
Hello John! What is going on?
As you can see in the above example, a space is added instead of a new line.
Python print without newline in Python 2.x
If we want to print the statements in the same line in Python 2.x, we can use a comma at the end of our print statement to move our text onto the same line. The following example demonstrates this-
print("Good Morning! "),
print("How are you?")
Output of the above code -
Good Morning! How are you?
Python sys module
We can also print without a newline by using the sys module. This module contains many functions and variables to work in the runtime environment. This module provides the sys.stdout.write() function to print without a newline. So, first we need to import the module sys using the import keyword and then use the sys.stdout.write() function to print without a new line and without blank spaces.
import sys
sys.stdout.write("Hello John! ")
sys.stdout.write("How are you?")
Output of the above code -
Hello John! How are you?
Related Articles
Python program to display calendar
Sum of n numbers in Python using for loop
Sum of n numbers in Python using while loop
Python program to multiply two numbers
Python program to print odd numbers within a given range
Convert list to dictionary Python
Convert array to list Python
numpy dot product
glob in Python
Python heap implementation
zip function in Python
Remove last element from list Python
Check if list is empty Python
Remove element from list Python
Python split multiple delimiters
Python loop through list
Python iterate list with index
Python add list to list
Python random choice
Python dict inside list
Remove character from string Python
Python raise keyword