To print without a new line in Python 3 add an extra argument to your print function telling the program that you don’t want your next string to be on a new line. Here’s an example:
print("Hello there!", end = '') The next print function will be on the same line.
When you’re programming, you may want to print a value to the screen, but keep it on the same line as the last value you printed. For example, you may want a user’s first name and last name to appear on the same line. But how do you print without newline in Python?
In this tutorial, we are going to break down how printing without a new line in Python works. Before we get started, though, let’s explore the basics of how strings work in Python.
Strings are a sequence of letters, numbers, symbols, and spaces. Strings are an important data type in most programming languages, especially Python. When you’re working with strings, there are many cases where you will want to make them more readable through punctuation, adding new lines, or moving text onto the same lines.
In Python, strings are defined as follows:
ourString = "This is our string!"
Alternatively, you can use single quotes
‘’ to declare your string, as long as the string starts and ends with the same type of quote. However, if your string contains any single quotes, you may want to use double quotes instead and vice versa, otherwise your string will return an error.
How to Print Without Newline in Python
Typically, when you’re printing strings in Python, they will appear on different lines. Here’s an example:
print("First test string.") print("Second test string.")
This code would return the following:
First test string. Second test string.
What if we want both of these strings to appear on the same line? Let’s break down the two methods you can use to achieve this goal in both Python 2.x and Python 3.x.
Print Without Newline in Python 3.x
Printing without a new line is simple in Python 3. In order to print without newline in Python, you need to add an extra argument to your print function that will tell the program that you don’t want your next string to be on a new line.
Here’s an example:
print("Hello there!", end = '') print("It is a great day.")
The output for our code is:
Hello there!It is a great day.
Now we have our code printed on the same line.
Notice that our code did not add a space between our two strings. But what if we want a space between our two new strings that are on the same line? All we have to do is add a space character in our
In the above example, we had an empty string in our quote marks, which means that no spaces were added between our text on the new line. However, if we had added a space in the quote marks, we would have one in our code. In addition, you can add in whatever you want to the end parameter, and that will be what separates your strings on the new line.
print("Hello there!", end = ' It's Friday. ') print("It is a great day.")
Our code would return:
Hello there! It's Friday. It is a great day.
Print Without Newline in Python 2.x
If you want to print your text on the same line in Python 2, you should use a comma at the end of your print statement. Here’s an example of this in action:
print "Hello there!", print "It is a great day."
This code will return the following:
Hello there! It is a great day.
It is worth noting that, even though there were no new lines, a space character was still used to divide our text. This is useful because in most cases we will want our data to be spaced out. If we’re merging two sentences, for example, we would want a space between them.
That said, if you want to print text without a newline and without a space character, there is another approach you can use. By using the
sys module—which comes installed with Python—you are able to print exactly what you ask the program to print. Here is an example:
import sys sys.stdout.write("Hello there!") sys.stdout.write("It is a great day.")
The output for our code is as follows:
Hello there!It is a great day.
There is a lot going on in this code, so let’s break it down. On the first line, we import our
sys library, which has the function we need to print without a new line and without blank spaces. In the following lines, we tell the system to print out our text exactly as it is, without any spaces. So, we end up with the output we got above: our two strings on the same line, but with no space dividing them.
It’s that simple! In this tutorial, we have broken down how to print without newline in Python. As we have discussed, this programming technique can be useful if you need to display your data in a particular way that involves displaying all your text on the same line.
In Python 2.x, we can add a comma to the end of our print statement to move our text onto the same line, and in Python 3.x we need to add an
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.