How to Print Without Newline in Python 3

Published Feb 3, 2020

TL;DR

print('*', end='')

How Does It Work?

Every programming language allows us to print text to the screen.

In some ways, you can argue that Python has the simplest and most intuitive method of printing.

# Python
print("Hello world")
// C++
cout << "Hello world";
# Ruby
puts "Hello world"
// Java
System.out.println("Hello world");
// JavaScript
console.log("Hello world")
// C#
Console.WriteLine("Hello world");

However, the simplicity comes with its own issues.

Examples in Python 3

Check out this Python program and its output to the right:

print("Hello! ")
print("Life should be so much ")
print("simpler")
Hello!
Life should be so much
simpler

Each of these print statements outputs to separate lines instead of the same one.

Now, of course, there’s no reason I couldn’t have simply printed this:

print("Hello! Life should be so much simpler")
Hello! Life should be so much simpler

But, depending on the situation, we may need to print with separate print statements.

Let’s say we wanted to print a single row of asterisks. We may think to write it like this:

for i in range(5):
   print('*')

Unfortunately, this gives us:

*
*
*
*
*

So… what can we do?

Explanation

The first thing to understand is what we call an escape character.

These escape characters are written as normal text within strings represent symbols.

They are denoted using a backslash \.

For example, the following two examples output the same result:

print("    Corgi")
print("\tCorgi")

Both examples will output four spaces before Corgi. This is because \t represents a tab when used inside a string.

The important feature to note is that the literal \t is never printed.

Newlines, or linefeeds, are expressed this way as well with \n.

Anytime there is a newline at the end of a string of text, that means the string looks like Corgi\n instead of Corgi.

In Python 3, the print() function adds this automatically, almost as if every line looks like this:

print("Hello! \n")
print("Life should be so much \n")
print("simpler\n")

Instead, Python has a second parameter end that denotes the ending character for us. It defaults to \n. This means that every single print() function we write actually looks like this:

print("Hello! ", end='\n')
print("Life should be so much ", end='\n')
print("simpler", end='\n')

Solution

The solution is simple! We can simply change the end parameter to whatever we’d like.

If we wanted to end every single line with a tab, we could do the following:

print("Hello! ", end='\t')
print("Life should be so much ", end='\t')
print("simpler", end='\t')

But, given the title of this article, we probably want to remove any ending character sequence. We can do that like so:

print("Hello! ", end='')
print("Life should be so much ", end='')
print("simpler", end='')

Finally, we can print our row of asterisks without newlines in between each one.

for x in range(1,10):
   print('*', end='')
****

Conclusion

Python 3 is simple to start, but stuff like printing without newlines can be confusing to figure out on your own.

Be sure to remember that print() can take in more parameters and that end will denote how we want to terminate our string.

Good luck!


More Python Articles