How to Print Without Newline in Python 3
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");
// 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?
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
Anytime there is a newline at the end of a string of text, that means the string looks like
Corgi\n instead of
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')
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='')
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.
More Python Articles
- How to Start an HTTP Server in Python
- How to Get All Keys with the Highest Value in Python
- How to Check if a Tuple Exists in a List in Python
- How to Sort a List of Dictionaries By Field in Python
- How to Sort a Dictionary by Value in Python
- How to Flatten a 2D List in Python
- How to Sort a List of Tuples Based on Multiple Elements
- How to Remove Duplicates from a List in Python
- How to Set Multiple Values of a List in Python
- How to Remove the Last N Elements of a List in Python
- How to Get the ASCII Value of a Character in Python
- How to Loop Over a String in Reverse in Python
- How to Create a Two Dimensional List in Python
- How to Migrate Data from MongoDB to Elasticsearch in Python
- How to Add Key-Value to Dictionary During List Comprehension in Python
- How to Fix "datetime is not JSON serializable" TypeError in Python
- How to Remove a Key From a Dictionary in Python
- Encoding and Decoding Bytes Explained
- How to Paginate/Scroll Elasticsearch Data using Python
- How to Get the Key with the Maximum Value in Python
- List Comprehension in Python Explained Visually
- How to Check if a String Contains Another String in Python
- What is Enumerate in Python?
- What is the Difference Between List and Tuple in Python?