{"id":268,"date":"2024-10-15T08:01:05","date_gmt":"2024-10-15T08:01:05","guid":{"rendered":"https:\/\/itsmycode.com\/?p=268"},"modified":"2024-10-15T08:01:05","modified_gmt":"2024-10-15T08:01:05","slug":"python-remove-newline-from-string","status":"publish","type":"post","link":"https:\/\/itsmycode.com\/python-remove-newline-from-string\/","title":{"rendered":"Python Remove Newline From String"},"content":{"rendered":"\n<p>There are times where we need to remove the newline from string while processing massive data. This tutorial will learn different approaches to strip newline characters from string in Python.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"python-remove-newline-from-string\">Python Remove Newline From String<\/h2>\n\n\n\n<p>In Python new line character is represented with \u201c<strong><code>\\n<\/code><\/strong>.\u201d Python\u2019s print statement by default adds the newline character at the end of the string.<\/p>\n\n\n\n<p>There are 3 different methods to remove the newline characters from the string.&nbsp;<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>strip() method<\/strong><\/li>\n\n\n\n<li><strong>replace() method<\/strong><\/li>\n\n\n\n<li><strong>re.sub() method<\/strong><\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"using-strip-method-to-remove-the-newline-character-from-a-string\">Using strip() method to remove the newline character from a string<\/h2>\n\n\n\n<p>The <strong><code>strip()<\/code><\/strong> method will remove both trailing and leading newlines from the string. It also removes any whitespaces on both sides of a string.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># strip() method to remove newline characters from a string\ntext= \"\\n Welcome to Python Programming \\n\"\nprint(text.strip())\n<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Welcome to Python Programming<\/code><\/pre>\n\n\n\n<p>If the newline is at the end of the string, you could use the <strong><code>rstrip()<\/code><\/strong> method to remove a trailing newline characters from a string, as shown below.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># rstrip() method to remove trailing newline character from a string\ntext= \"Welcome to Python Programming \\n\"\nprint(text.rstrip())\n<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Welcome to Python Programming<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"using-replace-method-to-remove-newlines-from-a-string\">Using replace() method to remove newlines from a string<\/h2>\n\n\n\n<p>The <strong><code>replace()<\/code><\/strong> function is a built-in method, and it will replace the specified character with another character in a given string.&nbsp;<\/p>\n\n\n\n<p>In the below code, we are using <code>replace()<\/code> function to replace the newline characters in a given string. The <strong><code>replace()<\/code><\/strong> function will replace the old character and substitute it with an empty one.<\/p>\n\n\n\n<p>Similarly, if we need to replace inside newline characters in a list of strings, we can iterate it through for loop and use a <strong><code>replace()<\/code><\/strong> function to remove the newline characters.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Python code to remove newline character from string using replace() method\n\ntext = \"A regular \\n expression is a sequence \\n of characters\\n that specifies a search\\n pattern.\"\nprint(text.replace('\\n', ''))\n\nmy_list = &#91;\"Python\\n\", \"is\\n\", \"Fun\\n\"]\nnew_list = &#91;]\n\nprint(\"Original List: \", my_list)\n\nfor i in my_list:\n    new_list.append(i.replace(\"\\n\", \"\"))\nprint(\"After removal of new line \", new_list)\n<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>A regular  expression is a sequence  of characters that specifies a search pattern. \nOriginal List:  &#91;'Python\\n', 'is\\n', 'Fun\\n']\nAfter removal of new line  &#91;'Python', 'is', 'Fun']<\/code><\/pre>\n\n\n\n<p>We can also use the map function in Python to iterate the list of strings and remove the newline characters, as shown below. It would be a more optimized and efficient way of coding when compared to the for a loop.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>my_list = &#91;\"Python\\n\", \"is\\n\", \"Fun\\n\"]\nprint(list(map(str.strip, my_list)))\n<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;'Python', 'is', 'Fun']<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"using-regex-to-remove-newline-character-from-string\">Using regex to remove newline character from string&nbsp;&nbsp;<\/h2>\n\n\n\n<p>Another approach is to use the regular expression functions in Python to replace the newline characters with an empty string. The regex approach can be used to remove all the occurrences of the newlines in a given string.<\/p>\n\n\n\n<p>The <strong><code>re.sub()<\/code><\/strong> function is similar to <strong><code>replace()<\/code><\/strong> method in Python. The re.sub() function will replace the specified newline character with an empty character.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Python code to remove newline character from string using regex\n\nimport re\ntext = \"A regular \\n expression is a sequence \\n of characters\\n that specifies a search\\n pattern.\"\nprint(re.sub('\\n', '', text))\n\nmy_list = &#91;\"Python\\n\", \"is\\n\", \"Fun\\n\"]\nnew_list = &#91;]\n\nprint(\"Original List: \", my_list)\n\nfor i in my_list:\n    new_list.append(re.sub(\"\\n\", \"\", i))\nprint(\"After removal of new line \", new_list)\n<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>A regular  expression is a sequence  of characters that specifies a search pattern. \nOriginal List:  &#91;'Python\\n', 'is\\n', 'Fun\\n']\nAfter removal of new line  &#91;'Python', 'is', 'Fun']<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>There are times where we need to remove the newline from string while processing massive data. This tutorial will learn different approaches to strip newline characters from string in Python. Python Remove Newline From String In Python new line character is represented with \u201c\\n.\u201d Python\u2019s print statement by default adds the newline character at the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":269,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16,1],"tags":[],"class_list":["post-268","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-basics","category-python"],"_links":{"self":[{"href":"https:\/\/itsmycode.com\/wp-json\/wp\/v2\/posts\/268","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itsmycode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itsmycode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itsmycode.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/itsmycode.com\/wp-json\/wp\/v2\/comments?post=268"}],"version-history":[{"count":1,"href":"https:\/\/itsmycode.com\/wp-json\/wp\/v2\/posts\/268\/revisions"}],"predecessor-version":[{"id":270,"href":"https:\/\/itsmycode.com\/wp-json\/wp\/v2\/posts\/268\/revisions\/270"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/itsmycode.com\/wp-json\/wp\/v2\/media\/269"}],"wp:attachment":[{"href":"https:\/\/itsmycode.com\/wp-json\/wp\/v2\/media?parent=268"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itsmycode.com\/wp-json\/wp\/v2\/categories?post=268"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itsmycode.com\/wp-json\/wp\/v2\/tags?post=268"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}