{"id":126197,"date":"2022-07-02T14:20:17","date_gmt":"2022-07-02T14:20:17","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=451007"},"modified":"2022-07-02T14:20:17","modified_gmt":"2022-07-02T14:20:17","slug":"how-to-skip-a-line-in-python-using-n","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2022\/07\/02\/how-to-skip-a-line-in-python-using-n\/","title":{"rendered":"How to Skip a Line in Python using \\n?"},"content":{"rendered":"<div class=\"kk-star-ratings kksr-valign-top kksr-align-left \" data-payload=\"{&quot;align&quot;:&quot;left&quot;,&quot;id&quot;:&quot;451007&quot;,&quot;slug&quot;:&quot;default&quot;,&quot;valign&quot;:&quot;top&quot;,&quot;reference&quot;:&quot;auto&quot;,&quot;count&quot;:&quot;1&quot;,&quot;readonly&quot;:&quot;&quot;,&quot;score&quot;:&quot;5&quot;,&quot;best&quot;:&quot;5&quot;,&quot;gap&quot;:&quot;5&quot;,&quot;greet&quot;:&quot;Rate this post&quot;,&quot;legend&quot;:&quot;5\\\/5 - (1 vote)&quot;,&quot;size&quot;:&quot;24&quot;,&quot;width&quot;:&quot;142.5&quot;,&quot;_legend&quot;:&quot;{score}\\\/{best} - ({count} {votes})&quot;}\">\n<div class=\"kksr-stars\">\n<div class=\"kksr-stars-inactive\">\n<div class=\"kksr-star\" data-star=\"1\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"2\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"3\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"4\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"5\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div class=\"kksr-stars-active\" style=\"width: 142.5px;\">\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/div>\n<div class=\"kksr-legend\"> 5\/5 &#8211; (1 vote) <\/div>\n<\/div>\n<div class=\"wp-block-cover is-light\"><span aria-hidden=\"true\" class=\"wp-block-cover__background has-background-dim\"><\/span><img loading=\"lazy\" decoding=\"async\" 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=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/p>\n<div class=\"wp-block-cover__inner-container\">\n<p class=\"has-text-align-center has-base-3-color has-text-color\" style=\"font-size:100px\"><strong>Skip Line \\n<\/strong><\/p>\n<\/div>\n<\/div>\n<p><strong>Summary:<\/strong><\/p>\n<ul class=\"has-global-color-8-background-color has-background\">\n<li>Python&#8217;s newline character<code> \\n<\/code> indicates the end of a line of text.<\/li>\n<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>\n<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>\n<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>\n<\/ul>\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n<p>Python&#8217;s newline character to indicate the end of a line of text is <code>\\n<\/code>.<\/p>\n<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>\n<pre class=\"wp-block-preformatted\">PYTHON CODE:\n<code>print('hello\\nworld\\n\\nPython is great!')<\/code> OUTPUT:\n<code>hello\nworld Python is great!<\/code><\/pre>\n<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>\n<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=\"\">#################################\n# File: my_filename.txt #\n#################################\n# My #\n# File #\n# Content #\n################################# with open('my_filename.txt', 'r') as my_file: for line in my_file.readlines(): print(line) # Output:\nMy\nFile\nContent<\/pre>\n<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>\n<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>\n<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:\nMy File Content # End Output<\/pre>\n<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>\n<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:\nMy File Content # End Output<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>5\/5 &#8211; (1 vote) Skip Line \\n Summary: Python&#8217;s newline character \\n indicates the end of a line of text. The built-in print() function automatically adds a newline character \\n at the end. You can customize this behavior of separating two lines using a single newline character &#8216;\\n&#8217; by changing the default end=&#8217;\\n&#8217; argument of [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[857],"tags":[73,468,528],"class_list":["post-126197","post","type-post","status-publish","format-standard","hentry","category-python-tut","tag-programming","tag-python","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/126197","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/comments?post=126197"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/126197\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=126197"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=126197"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=126197"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}