Python: Print without Newline
In this article, we’ll examine how to print a string without a newline character using Python.
In Python, the built-in print
function is used to print content to the standard output, which is usually the console. By default, the print function adds a newline character at the end of the printed content, so the next output by the program occurs on the next line.
Try running this code to see an example:
print('Banana')
print('pudding.')
Output:
Banana
pudding.
As expected, the output of each print
statement is shown on its own line.
However, in some cases we may want to output multiple strings on the same line using separate print
statements. There are a few ways to prevent Python from adding the newline character when using the print
function, depending on whether we are using Python 2.x or Python 3.x.
For example, this kind of functionality is useful for when you’re developing a REPL or any command line application that takes input from the user, and you don’t want the prompt and input text to be on different lines.
For Python 2.x, we can simply add a comma after the print function call, which will terminate the