Summary: To print without the newline character in Python 3, set the
end argument in the
print() function to the empty string or the single whitespace character. This ensures that there won’t be a newline in the standard output after each execution of
print(). Alternatively, unpack the iterable into the
print() function to avoid the use of multiple
Let’s go over this problem and these two solutions step-by-step.
Problem: How to use the
print() function without printing an implicit newline character to the Python shell?
Example: Say, you want to use the
print() function within a for loop—but you don’t want to see multiple newlines between the printed output:
for i in range(1,5): print(i)
The default standard output is the following:
1 2 3 4
But you want to get the following output in a single line of Python code.
1 2 3 4
How to accomplish this in Python 3?
Solution: I’ll give you the quick solution in an interactive Python shell here:
By reading on, you’ll understand how this works and become a better coder in the process.
Let’s have a quick recap of the Python
Python Print Function – Quick Start Guide
There are two little-used arguments of the print function in Python.
- The argument
sepindicates the separator which is printed between the objects.
- The argument
enddefines what comes at the end of each line.
Related Article: Python Print Function [And Its SECRET Separator & End Arguments]
Consider the following example:
a = 'hello' b = 'world' print(a, b, sep=' Python ', end='!')
Try it yourself in our interactive code shell:
Exercise: Click “Run” to execute the shell and see the output. What has changed?
Solution 1: End Argument of Print Function
Having studied this short guide, you can now see how to solve the problem:
To print the output of the for loop to a single line, you need to define the end argument of the print function to be something different than the default newline character. In our example, we want to use the empty space after each string we pass into the
print() function. Here’s how you accomplish this:
for i in range(1,5): print(i, end=' ')
The shell output concentrates on a single line:
1 2 3 4
By defining the end argument, you can customize the output to your problem.
Solution 2: Unpacking
However, there’s an even more advanced solution that’s more concise and more Pythonic. It makes use of the unpacking feature in Python.
print(*range(1,5)) # 1 2 3 4
The asterisk prefix
* before the
range(1,5) unpacks all values in the
range iterable into the print function. This way, it becomes similar to the function execution
print(1, 2, 3, 4) with comma-separated arguments. You can use an arbitrary number of arguments in the
Per default, Python will print these arguments with an empty space in between. If you want to customize this separator string, you can use the
sep argument as you’ve learned above.
How to Print a List?
Do you want to print a list to the shell? Just follow these simple steps:
- Pass a list as an input to the
print()function in Python.
- Use the asterisk operator
*in front of the list to “unpack” the list into the print function.
- Use the
separgument to define how to separate two list elements visually.
Here’s the code:
# Create the Python List lst = [1, 2, 3, 4, 5] # Use three underscores as separator print(*lst, sep='___') # 1___2___3___4___5 # Use an arrow as separator print(*lst, sep='-->') # 1-->2-->3-->4-->5
Try It Yourself in Our Interactive Code Shell:
This is the best and most Pythonic way to print a Python list. If you still want to learn about alternatives—and improve your Python skills in the process of doing so—read the following tutorial!
Related Article: Print a Python List Beautifully [Click & Run Code]
Where to Go From Here?
Enough theory, let’s get some practice!
To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?
Practice projects is how you sharpen your saw in coding!
Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?
Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.
Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com. He’s author of the popular programming book Python One-Liners (NoStarch 2020), coauthor of the Coffee Break Python series of self-published books, computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.