08-25-2020, 04:12 PM
How to Print Without Newline in Python—A Simple Illustrated Guide
Summary: To print without the newline character in Python 3, set the argument in the 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 . Alternatively, unpack the iterable into the function to avoid the use of multiple statements: .
![[Image: printwithoutnewline-1024x576.jpg]](https://blog.finxter.com/wp-content/uploads/2020/08/printwithoutnewline-1024x576.jpg)
Let’s go over this problem and these two solutions step-by-step.
Problem: How to use the function without printing an implicit newline character to the Python shell?
Example: Say, you want to use the function within afor loop—but you don’t want to see multiple newlines between the printed output:
The default standard output is the following:
But you want to get the following output in a single line of Python code.
How to accomplish this in Python 3?
Solution: I’ll give you the quick solution in an interactive Python shell here:
https://repl.it/@finxter/ClearDirectAuto...?lite=true
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 function!
Python Print Function – Quick Start Guide
There are two little-used arguments of the print function in Python.
Related Article: Python Print Function [And Its SECRET Separator & End Arguments]
Consider the following example:
Try it yourself in our interactive code shell:
https://repl.it/@finxter/printfunctionexample?lite=true
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 function. Here’s how you accomplish this:
The shell output concentrates on a single line:
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.
The asterisk prefix before the unpacks all values in the iterable into the print function. This way, it becomes similar to the function execution with comma-separated arguments. You can use an arbitrary number of arguments in the function.
Per default, Python will printthese arguments with an empty space in between. If you want to customize this separator string, you can use the 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:
Here’s the code:
Try It Yourself in Our Interactive Code Shell:
https://repl.it/@finxter/pythonlistprint3?lite=true
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.
Join the free webinar now!
Summary: To print without the newline character in Python 3, set the
Code:
endCode:
print()Code:
print()Code:
print()Code:
print()Code:
print(*iter)![[Image: printwithoutnewline-1024x576.jpg]](https://blog.finxter.com/wp-content/uploads/2020/08/printwithoutnewline-1024x576.jpg)
Let’s go over this problem and these two solutions step-by-step.
Problem: How to use the
Code:
print()Example: Say, you want to use the
Code:
print()Code:
for i in range(1,5): print(i)The default standard output is the following:
Code:
1
2
3
4But you want to get the following output in a single line of Python code.
Code:
1 2 3 4How to accomplish this in Python 3?
Solution: I’ll give you the quick solution in an interactive Python shell here:
https://repl.it/@finxter/ClearDirectAuto...?lite=true
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
Code:
print()Python Print Function – Quick Start Guide
There are two little-used arguments of the print function in Python.
- The argument indicates the separator which is printed between the objects.Code:
sep
- The argument defines what comes at the end of each line.Code:
end
Related Article: Python Print Function [And Its SECRET Separator & End Arguments]
Consider the following example:
Code:
a = 'hello'
b = 'world' print(a, b, sep=' Python ', end='!')Try it yourself in our interactive code shell:
https://repl.it/@finxter/printfunctionexample?lite=true
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
Code:
print()Code:
for i in range(1,5): print(i, end=' ')The shell output concentrates on a single line:
Code:
1 2 3 4By 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.
Code:
print(*range(1,5))
# 1 2 3 4The asterisk prefix
Code:
*Code:
range(1,5)Code:
rangeCode:
print(1, 2, 3, 4)Code:
print()Per default, Python will printthese arguments with an empty space in between. If you want to customize this separator string, you can use the
Code:
sepHow 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 function in Python.Code:
print()
- Use the asterisk operatorin front of the list to “unpack” the list into the print function.Code:
*
- Use the argument to define how to separate two list elements visually.Code:
sep
Here’s the code:
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-->5Try It Yourself in Our Interactive Code Shell:
https://repl.it/@finxter/pythonlistprint3?lite=true
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.
Join the free webinar now!

