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 print()
statements: print(*iter)
.
Let’s go over this problem and these two solutions step-by-step.
Problem Formulation
💬 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)
Output: The default output is the following:
1 2 3 4
Desired Output: 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?
Let’s have a quick recap of the Python print()
function!
Python Print Function – Quick Start Guide
There are two little-used arguments of the print()
function in Python.
- The argument
sep
indicates the separator which is printed between the objects. - The argument
end
defines 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='!') # hello Python world!
To print without newline, you have to do nothing else but override the end
argument of the Python print()
function. Just replace the default end='\n'
with your desired ending string — or the empty string end=''
if you don’t want to have any ending character.
Solution 1: End Argument of Print Function
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.
Feel free to learn more about these customizations of the print()
function in the following video:
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 print()
function.
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.
Related Challenge: 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
sep
argument 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
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]
If you want to learn how to print a list without newline, feel free to check out my specific article on the Finxter blog covering this topic in detail.
Where to Go From Here?
Enough theory. Let’s get some practice!
Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation.
To become more successful in coding, solve more real problems for real people. 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?
You build high-value coding skills by working on practical coding projects!
Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?
🚀 If your answer is YES!, consider becoming 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.
If you just want to learn about the freelancing opportunity, feel free to watch my free webinar “How to Build Your High-Income Skill Python” and learn how I grew my coding business online and how you can, too—from the comfort of your own home.