Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] How to Print Without Newline in Python—A Simple Illustrated Guide
1
How to Print Without Newline in Python—A Simple Illustrated Guide

Summary: To print without the newline character in Python 3, set the
Code:
end
argument in the
Code:
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
Code:
print()
. Alternatively, unpack the iterable into the
Code:
print()
function to avoid the use of multiple
Code:
print()
statements:
Code:
print(*iter)
.

[Image: printwithoutnewline-1024x576.jpg]

Let’s go over this problem and these two solutions step-by-step.

Problem: How to use the
Code:
print()
function
without printing an implicit newline character to the Python shell?

Example: Say, you want to use the
Code:
print()
function within afor loop—but you don’t want to see multiple newlines between the printed output:

Code:
for i in range(1,5): print(i)

The default standard output is the following:

Code:
1 2 3 4

But you want to get the following output in a single line of Python code.

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:

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()
function!

Python Print Function – Quick Start Guide



There are two little-used arguments of the print function in Python.
  • The argument
    Code:
    sep
    indicates the separator which is printed between the objects.


  • The argument
    Code:
    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:

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()
function. Here’s how you accomplish this:

Code:
for i in range(1,5): print(i, end=' ')

The shell output concentrates on a single line:

Code:
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.

Code:
print(*range(1,5)) # 1 2 3 4

The asterisk prefix
Code:
*
before the
Code:
range(1,5)
unpacks all values in the
Code:
range
iterable into the print function. This way, it becomes similar to the function execution
Code:
print(1, 2, 3, 4)
with comma-separated arguments. You can use an arbitrary number of arguments in the
Code:
print()
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
Code:
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
    Code:
    print()
    function in Python.

  • Use the asterisk operator
    Code:
    *
    in front of the list to “unpack” the list into the print function.

  • Use the
    Code:
    sep
    argument to define how to separate two list elements visually.

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-->5

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!
Reply

Users browsing this thread:
1 Guest(s)


Forum Jump: