In Python, the print
function is used to display output on the console.
By default, print
adds a newline character at the end of the output, which means that the next output will start on a new line.
However, there are cases when we want to print output without a newline, either to format our output or to print multiple items on the same line.
In this tutorial, we will discuss how to print output without a newline in Python.
To print output without a newline in Python, we need to use the end
parameter of the print
function.
The end
parameter specifies what character should be used to separate the output items.
By default, end
is set to a newline character, but we can change it to any character we want.
Here is an example program that demonstrates how to print output without a newline in Python:
# Print multiple items on the same line print("Hello", end=" ") print("world!") # Print output without a newline print("This is", end=" ") print("a sentence", end=" ") print("without", end=" ") print("a newline.")
In the first example, we use the end
parameter to specify a space character as the separator between the two items, so they are printed on the same line.
In the second example, we use the end
parameter to print multiple items without a newline character between them, resulting in a single line output.
It is important to note that when using end
to print output without a newline, we should make sure to add a space or another separator character between the items, otherwise, they will be printed without any separation, which might make the output difficult to read.
In conclusion, printing output without a newline in Python is straightforward.
We just need to use the end
parameter of the print
function and specify the separator character we want to use.
By doing this, we can format our output and print multiple items on the same line.