[Tut] How to Skip a Line in Python using \n? - Printable Version +- Sick Gaming (https://www.sickgaming.net) +-- Forum: Programming (https://www.sickgaming.net/forum-76.html) +--- Forum: Python (https://www.sickgaming.net/forum-83.html) +--- Thread: [Tut] How to Skip a Line in Python using \n? (/thread-99667.html) |
[Tut] How to Skip a Line in Python using \n? - xSicKxBot - 07-05-2022 How to Skip a Line in Python using \n? <div><div class="kk-star-ratings kksr-valign-top kksr-align-left " data-payload="{"align":"left","id":"451007","slug":"default","valign":"top","reference":"auto","count":"1","readonly":"","score":"5","best":"5","gap":"5","greet":"Rate this post","legend":"5\/5 - (1 vote)","size":"24","width":"142.5","_legend":"{score}\/{best} - ({count} {votes})"}"> <div class="kksr-stars"> <div class="kksr-stars-inactive"> <div class="kksr-star" data-star="1" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" data-star="2" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" data-star="3" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" data-star="4" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" data-star="5" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> </p></div> <div class="kksr-stars-active" style="width: 142.5px;"> <div class="kksr-star" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> </p></div> </div> <div class="kksr-legend"> 5/5 – (1 vote) </div> </div> <div class="wp-block-cover is-light"><span aria-hidden="true" class="wp-block-cover__background has-background-dim"></span><img loading="lazy" width="1024" height="536" class="wp-block-cover__image-background wp-image-451029" alt="" src="https://blog.finxter.com/wp-content/uploads/2022/07/python-logo-generic-reddit-1024x536.jpg" data-object-fit="cover" srcset="https://blog.finxter.com/wp-content/uploads/2022/07/python-logo-generic-reddit-1024x536.jpg 1024w, https://blog.finxter.com/wp-content/uploads/2022/07/python-logo-generic-reddit-300x157.jpg 300w, https://blog.finxter.com/wp-content/uploads/2022/07/python-logo-generic-reddit-768x402.jpg 768w, https://blog.finxter.com/wp-content/uploads/2022/07/python-logo-generic-reddit.jpg 1200w" sizes="(max-width: 1024px) 100vw, 1024px" /></p> <div class="wp-block-cover__inner-container"> <p class="has-text-align-center has-base-3-color has-text-color" style="font-size:100px"><strong>Skip Line \n</strong></p> </div> </div> <p><strong>Summary:</strong></p> <ul class="has-global-color-8-background-color has-background"> <li>Python’s newline character<code> \n</code> indicates the end of a line of text.</li> <li>The built-in <code><a rel="noreferrer noopener" href="https://blog.finxter.com/python-print/" data-type="post" data-id="20731" target="_blank">print()</a></code> function automatically adds a <a rel="noreferrer noopener" href="https://blog.finxter.com/how-to-print-without-newline-in-python-a-simple-illustrated-guide/" data-type="post" data-id="12199" target="_blank">newline character</a> <code>\n</code> at the end.</li> <li>You can <a href="https://blog.finxter.com/the-separator-and-end-arguments-of-the-python-print-function/" data-type="post" data-id="4645" target="_blank" rel="noreferrer noopener">customize</a> this behavior of separating two lines using a single newline character <code>'\n'</code> by changing the default <code>end='\n'</code> argument of the <code>print()</code> function to your desired string. </li> <li>Another way to skip a line in the Python output is to add an empty <code>print()</code> statement that will just print an empty line and do nothing else. </li> </ul> <hr class="wp-block-separator has-alpha-channel-opacity"/> <p>Python’s newline character to indicate the end of a line of text is <code>\n</code>.</p> <p>If you print a string to the shell using the built-in <code><a href="https://blog.finxter.com/python-print/" data-type="post" data-id="20731" target="_blank" rel="noreferrer noopener">print()</a></code> function, Python automatically adds a <a href="https://blog.finxter.com/how-to-print-without-newline-in-python-a-simple-illustrated-guide/" data-type="post" data-id="12199" target="_blank" rel="noreferrer noopener">newline character</a> <code>\n</code> at the end.</p> <pre class="wp-block-preformatted">PYTHON CODE: <code>print('hello\nworld\n\nPython is great!')</code> OUTPUT: <code>hello world Python is great!</code></pre> <p>For example, if you iterate over the text in a file using a <code>for</code> <a href="https://blog.finxter.com/python-loops/" data-type="post" data-id="4596" target="_blank" rel="noreferrer noopener">loop</a> and print each line in the loop body, the lines are separated with single new lines.</p> <pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">################################# # File: my_filename.txt # ################################# # My # # File # # Content # ################################# with open('my_filename.txt', 'r') as my_file: for line in my_file.readlines(): print(line) # Output: My File Content</pre> <p>You can customize this behavior of separating two lines using a single newline character <code>'\n'</code> by changing the default <code>end='\n'</code> argument of the <code>print()</code> function to your desired string. </p> <p>For example, you can skip two lines in Python using <code>print(my_string, end='\n\n')</code> by chaining two newline characters <code>'\n\n'</code>. </p> <pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="3,7,9,11" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">with open('my_filename.txt', 'r') as my_file: for line in my_file.readlines(): print(line, end='\n\n') # Output: My File Content # End Output</pre> <p>Another way to skip a line in the Python output is to add an empty <code>print()</code> statement that will just print an empty line and do nothing else. </p> <pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="3,4,8,10,12" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">with open('my_filename.txt', 'r') as my_file: for line in my_file.readlines(): print(line) print() # Output: My File Content # End Output</pre> </div> https://www.sickgaming.net/blog/2022/07/02/how-to-skip-a-line-in-python-using-n/ |