Create an account


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

<div><p class="has-pale-cyan-blue-background-color has-background"><strong>Summary</strong>: To print without the newline character in Python 3, set the <code>end</code> argument in the <code>print()</code> 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()</code>. Alternatively, unpack the iterable into the <code>print()</code> function to avoid the use of multiple <code>print()</code> statements: <code>print(*iter)</code>.</p>
<div class="wp-block-image">
<figure class="aligncenter size-large is-resized"><img src="https://blog.finxter.com/wp-content/uploads/2020/08/printwithoutnewline-1024x576.jpg" alt="Print Without Newline Python (End Argument)" class="wp-image-12224" width="768" height="432" srcset="https://blog.finxter.com/wp-content/uploads/2020/08/printwithoutnewline-scaled.jpg 1024w, https://blog.finxter.com/wp-content/uplo...00x169.jpg 300w, https://blog.finxter.com/wp-content/uplo...68x432.jpg 768w, https://blog.finxter.com/wp-content/uplo...150x84.jpg 150w" sizes="(max-width: 768px) 100vw, 768px" /></figure>
</div>
<p>Let’s go over this problem and these two solutions step-by-step.</p>
<p><strong>Problem</strong>: How to use the <a href="https://blog.finxter.com/the-separator-and-end-arguments-of-the-python-print-function/" title="Python Print Function [And Its SECRET Separator &amp; End Arguments]" target="_blank" rel="noreferrer noopener"><code>print()</code> function</a> without printing an implicit newline character to the Python shell?</p>
<p><strong>Example</strong>: Say, you want to use the <code>print()</code> function within a<a href="https://blog.finxter.com/python-one-line-for-loop-a-simple-tutorial/" target="_blank" rel="noreferrer noopener" title="Python One Line For Loop [A Simple Tutorial]"> for loop</a>—but you don’t want to see multiple newlines between the printed output:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">for i in range(1,5): print(i)</pre>
<p>The default standard output is the following:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">1
2
3
4</pre>
<p>But you want to get the following output in a single line of Python code.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">1 2 3 4</pre>
<p>How to accomplish this in Python 3?</p>
</p>
<p><strong>Solution</strong>: I’ll give you the quick solution in an interactive Python shell here:</p>
<p> <iframe height="400px" width="100%" src="https://repl.it/@finxter/ClearDirectAutoresponder?lite=true" scrolling="no" frameborder="no" allowtransparency="true" allowfullscreen="true" sandbox="allow-forms allow-pointer-lock allow-popups allow-same-origin allow-scripts allow-modals"></iframe> </p>
<p>By reading on, you’ll understand how this works and become a better coder in the process.</p>
<p>Let’s have a quick recap of the Python <code>print()</code> function!</p>
<h2>Python Print Function – Quick Start Guide</h2>
<figure class="wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio">
<div class="wp-block-embed__wrapper">
<div class="ast-oembed-container"><iframe title="Python Print Function [And Its SECRET Separator &amp; End Arguments]" width="1400" height="788" src="https://www.youtube.com/embed/yXhEvg8Domk?feature=oembed" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe></div>
</div>
</figure>
<p><strong><em>There are two little-used arguments of the print function in Python. </em></strong></p>
<ul>
<li><strong><em>The argument <code>sep</code> indicates the separator which is printed between the objects.</em></strong></li>
<li><strong><em>The argument <code>end</code> defines what comes at the end of each line.</em></strong></li>
</ul>
<p><strong>Related Article:</strong> <a href="https://blog.finxter.com/the-separator-and-end-arguments-of-the-python-print-function/" target="_blank" rel="noreferrer noopener" title="Python Print Function [And Its SECRET Separator &amp; End Arguments]">Python Print Function [And Its SECRET Separator &amp; End Arguments]</a></p>
<p>Consider the following example:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">a = 'hello'
b = 'world' print(a, b, sep=' Python ', end='!')</pre>
<p>Try it yourself in our interactive code shell:</p>
<p> <iframe src="https://repl.it/@finxter/printfunctionexample?lite=true" scrolling="no" allowtransparency="true" allowfullscreen="true" sandbox="allow-forms allow-pointer-lock allow-popups allow-same-origin allow-scripts allow-modals" width="100%" height="600px" frameborder="no"></iframe> </p>
<p><em><strong>Exercise</strong>: Click “Run” to execute the shell and see the output. What has changed</em>?</p>
<h2>Solution 1: End Argument of Print Function</h2>
<p>Having studied this short guide, you can now see how to solve the problem:</p>
<p>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> function. Here’s how you accomplish this:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">for i in range(1,5): print(i, end=' ')</pre>
<p>The shell output concentrates on a single line:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">1 2 3 4 </pre>
<p>By defining the end argument, you can customize the output to your problem. </p>
<h2>Solution 2: Unpacking</h2>
<p>However, there’s an even more advanced solution that’s more concise and more Pythonic. It makes use of the <a href="https://blog.finxter.com/what-is-asterisk-in-python/" target="_blank" rel="noreferrer noopener" title="What is the Asterisk / Star Operator (*) in Python?">unpacking feature</a> in Python. </p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">print(*range(1,5))
# 1 2 3 4</pre>
<p>The asterisk prefix <code>*</code> before the <code>range(1,5)</code> unpacks all values in the <a href="https://blog.finxter.com/daily-python-puzzle-range-indexing/" target="_blank" rel="noreferrer noopener" title="The Range Function and Indexing in Python"><code>range</code> </a>iterable into the print function. This way, it becomes similar to the function execution <code>print(1, 2, 3, 4)</code> with comma-separated arguments. You can use an arbitrary number of arguments in the <code>print()</code> function. </p>
<p>Per default, Python will <a href="https://blog.finxter.com/print-python-list/" title="Print a Python List Beautifully [Click &amp; Run Code]">print </a>these arguments with an empty space in between. If you want to customize this separator string, you can use the <code>sep</code> argument as you’ve learned above. </p>
<h2>How to Print a List?</h2>
<p>Do you want to print a list to the shell? Just follow these simple steps:</p>
<ul>
<li>Pass a list as an input to the <code>print()</code> function in Python. </li>
<li>Use the <a rel="noreferrer noopener" href="https://blog.finxter.com/what-is-asterisk-in-python/" target="_blank">asterisk operator </a><code>*</code> in front of the list to “unpack” the list into the print function. </li>
<li>Use the <code>sep</code> argument to define how to separate two list elements visually. </li>
</ul>
<p>Here’s the code:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group=""># 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</pre>
<p>Try It Yourself in Our Interactive Code Shell:</p>
<p> <iframe src="https://repl.it/@finxter/pythonlistprint3?lite=true" scrolling="no" allowtransparency="true" allowfullscreen="true" sandbox="allow-forms allow-pointer-lock allow-popups allow-same-origin allow-scripts allow-modals" width="100%" height="700px" frameborder="no"></iframe> </p>
<p>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!</p>
<p><strong>Related Article</strong>: <a href="https://blog.finxter.com/print-python-list/" target="_blank" rel="noreferrer noopener" title="Print a Python List Beautifully [Click &amp; Run Code]">Print a Python List Beautifully [Click &amp; Run Code]</a></p>
<h2>Where to Go From Here?</h2>
<p>Enough theory, let’s get some practice!</p>
<p>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?</p>
<p><strong>Practice projects is how you sharpen your saw in coding!</strong></p>
<p>Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?</p>
<p>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.</p>
<p>Join my free webinar <a rel="noreferrer noopener" href="https://blog.finxter.com/webinar-freelancer/" target="_blank">“How to Build Your High-Income Skill Python”</a> and watch how I grew my coding business online and how you can, too—from the comfort of your own home.</p>
<p><a href="https://blog.finxter.com/webinar-freelancer/" target="_blank" rel="noreferrer noopener">Join the free webinar now!</a></p></p>
</div>


https://www.sickgaming.net/blog/2020/08/...ted-guide/
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016