Jump straight to the solution:
Python is one of the easiest programming languages to learn.
One of the first programs that you write when you start learning any new programming language is a hello world program.
A hello world program in python looks like this
# hello world in python print("Hello World!")
It is that easy!
Just one line and boom you have your hello world program.
As you can see, it is a very simple function.
Yet there is one thing that is really annoying about this function.
It automatically prints a newline ‘\n’ at the end of the line!
Let’s take a look at this example
print("Hello World!") print("My name is Karim") # output: # Hello World! # My name is Karim
As you can notice, the two strings are not printed one after the other on the same line but on separate lines instead.
Even though this might be what you actually want, but that’s not always the case.
if you’re coming from another language, you might be more comfortable with explicitly mentioning whether a newline should be printed out or not.
For example in Java, you have to explicitly indicate your desire to print a newline by either using the println function or typing the newline character (\n) inside your print function:
// option 1 System.out.println("Hello World!") // option 2 System.out.print("Hello World!\n")
So what should we do if we want no newline characters in python?
Let’s jump right into the solution!
Printing with no newlines in Python 3
Python 3 provides the simplest solution, all you have to do is to provide one extra argument to the print function.
# use the named argument "end" to explicitly specify the end of line string print("Hello World!", end = '') print("My name is Karim") # output: # Hello World!My name is Karim
You can use the optional named argument end to explicitly mention the string that should be appended at the end of the line.
Whatever you provide as the end argument is going to be the terminating string.
So if you provide an empty string, then no newline characters, and no spaces will be appended to your input.
Printing with no newlines in Python 2
In python 2, the easiest way to avoid the terminating newline is to use a comma at the end of your print statement
# no newlines but a space will be printed out print "Hello World!", print "My name is Karim" # output # Hello World! My name is Karim
As you can see, even though there was no newlines, we still got a space character between the two print statements.
If you actually needed a space, then this is the simplest and most straightforward way to go.
But what if you want to print without a space or newline?
In this you can, you should use the all-powerful sys.stdout.write function from the sys module.
This function will only print whatever you explicitly tell it to print.
There are no terminating strings.
There is no magic!
Let’s take an example
import sys sys.stdout.write("Hello World!") sys.stdout.write("My name is Karim") # output # Hello World!My name is Karim
In Python 3, you can use the named end argument in the print function and assign an empty string to this argument to prevent the terminating newline.
In Python 2, you can either use a comma after your print statement if you don’t mind the space, or you can just use the sys.stdout.write() function
If you are a beginner, then I highly recommend this book.
No longer a beginner?
Then it’s time to take your Python skills to the next level with this book (It’s my favorite).
Are you Beginning your Programming Career?
I provide my best content for beginners in the newsletter.
- What programming language to start with?
- Do you need a CS degree to be a programmer?
- Career tips and advice
- Programming tutorials
And so much more…
Subscribe now. It’s Free.