While programming, we use the print()
function to print a string or a value to the standard output. Normally, each print()
function prints a value on a new line. However, we can change this behavior of the function. In this article, we will see how we can print without newline in python.
How to Print Without Newline in Python?
The print()
function takes various input arguments along with the values to print. Normally, when we do not pass any additional parameters to the print()
function, it prints values on a newline every time the function is called. You can observe this in the following example.
print("This is the first line.")
print("This is the second line.")
print("This is the third line.")
print("This is the fourth line.")
Output:
This is the first line.
This is the second line.
This is the third line.
This is the fourth line.
We can change this behavior of the print()
function by using the parameter “end
”. The parameter “end
” is used to specify the character. The print()
function prints after printing the values passed to it. The default value for the parameter “end
” is “\n
” i.e. newline character. This is why the print()
function prints a newline character after printing the values. Due to this, the next print()
statement always prints values on a new line.
You can pass any character as an input argument for the parameter “end
”. The print()
function, when executed, will always print the character passed as the input argument after printing the actual values. For instance, you can pass the period “.” character to the parameter “end
” as an input argument. This will make the print()
function print a period after printing the actual input values. You can observe this in the following example.
print("This is the first line.", end=".")
print("This is the second line.", end=".")
print("This is the third line.", end=".")
print("This is the fourth line.", end=".")
Output:
This is the first line..This is the second line..This is the third line..This is the fourth line..
To print a comma after each print statement execution, you can pass “,” as the input argument to the print()
function as follows.
print("This is the first line.", end=",")
print("This is the second line.", end=",")
print("This is the third line.", end=",")
print("This is the fourth line.", end=",")
Output:
This is the first line.,This is the second line.,This is the third line.,This is the fourth line.,
Similarly, you can pass a hyphen, &, *, $, @, !, parentheses, brackets, or any other character to the parameter “end
”. You can even pass the alphabets and numbers as input arguments to the print()
function. The print()
function will simply print the character passed to the parameter “end
” after printing the actual values without printing to a newline.
Conclusion
In this article, we have discussed how we can print without newline in python. You can also modify the appearance of strings while printing them by using string formatting in python. To learn more about strings in python, you may read this article on string concatenation.
Recommended Python Training
Course: Python 3 For Beginners
Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.