In this article, we'll examine how to print a string without a newline character using Python.
In Python, the built-in
Try running this code to see an example:
As expected, the output of each
However, in some cases we may want to output multiple strings on the same line using separate
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 printed string with a space instead of a newline character:
In Python 3.x, we can use the
end keyword argument in the
print('Banana', end=' ') print('pudding.')
So in this case, a space is used as the "termination" character, which results in the printed strings to be on the same line and only separated by a single space.
About the Author
This article was written by Jacob Stopak, a software consultant and developer with a passion for helping others improve their lives through code. Jacob is the creator of Code Card - a convenient tool for developers to look up, copy, and paste common code snippets.