{"id":19848,"date":"2022-05-18T11:40:51","date_gmt":"2022-05-18T06:10:51","guid":{"rendered":"https:\/\/java2blog.com\/?p=19848"},"modified":"2023-11-25T09:00:37","modified_gmt":"2023-11-25T03:30:37","slug":"replace-word-in-string-python","status":"publish","type":"post","link":"https:\/\/java2blog.com\/replace-word-in-string-python\/","title":{"rendered":"How to Replace Word in String in Python"},"content":{"rendered":"<p>In this tutorial, we will to how to replace word in string in Python.<\/p>\n<p>Strings represent a sequence of characters. In Python, they are immutable, which means that we cannot edit or modify strings. Whenever we perform any operation of modifying a string, we are creating a new copy. <\/p>\n<p>We will discuss how to replace word in string in Python in the methods below.<\/p>\n<h2>Using the <code>replace()<\/code> Function to replace word in string in Python<\/h2>\n<p>The <code>string<\/code> class in Python has many functionalities available to work with such objects. The <code>replace()<\/code> function belongs to this class and can be used to replace a given substring in a string.<\/p>\n<p>We can use this function to replace word in string in Python.<\/p>\n<p>For example,<\/p>\n<pre code=\"python\" title=\"Using replace() function\">\ns1 = \"java 2 blog\"\ns2 = s1.replace('java', 'python')\nprint(s2)    \n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"content-box-green\">\npython 2 blog\n<\/div>\n<p>In the above example, we create a string <code>s1<\/code> and replace a substring from this string using the <code>replace()<\/code> function. The new string is displayed.<\/p>\n<p>By default, all the possible occurrences of the substring are replaced using this function. However, it accepts a <code>count<\/code> parameter in which we can specify how many occurrences we want to replace.<\/p>\n<h2>Using the <code>re.Sub()<\/code> Function to replace word in string in Python<\/h2>\n<p>Regular expressions (regex) are used for reading and detecting substrings from a string based on a regex pattern. In Python, we can use the <code>re<\/code> module to work with regex.<\/p>\n<p>The <code>re.sub()<\/code> function reads a string and substitutes the substring that matches a given pattern with another substring.<\/p>\n<p>See the code below.<\/p>\n<pre code=\"python\" title=\"Using re.sub() function\">\nimport re\ns1 = \"java 2 blog\"\ns2 = re.sub('java', 'python', s1)\nprint(s2)    \n<\/pre>\n<p>Output:<\/p>\n<div class=\"content-box-green\">\npython 2 blog\n<\/div>\n<p>In the above example, we use the <code>re.sub()<\/code> function to replace word in string in Python. We can directly specify the substring to be replaced in the function rather than creating some elaborate pattern.<\/p>\n<p>We can use the <code>flags<\/code> parameter and specify its value as <code>re.IGNORECASE<\/code> to match the substring regardless of the case.<\/p>\n<p>Another way to use this function is by using the <code>re.compile()<\/code> function. This function can be used to compile a regex pattern first and be used with functions later on.<\/p>\n<p>We can use this to replace word in string in Python.<\/p>\n<p>For example,<\/p>\n<pre code=\"python\" title=\"Using re.compile()\">\nimport re\np = re.compile('java')\ns1 = \"java 2 blog\"\ns2 = p.sub('python',s1)\nprint(s2)    \n<\/pre>\n<p>Output:<\/p>\n<div class=\"content-box-green\">\npython 2 blog\n<\/div>\n<section class=\"read-more-posts\">\n<div class=\"rm-header\">\n<h2>Further reading:<\/h2>\n<\/div>\n<div class=\"rm-wrap\">\n<div class=\"rm-item\">\n<h5><a href=\"https:\/\/java2blog.com\/remove-word-from-string-python\/\" target=\"_blank\" rel=\"noopener\">Remove word from String in Python<\/a><\/h5>\n<div class=\"ex\">\n            <a href=\"https:\/\/java2blog.com\/remove-word-from-string-python\/\" target=\"_blank\" rel=\"noopener\">Read more \u2192<\/a>\n        <\/div>\n<\/div>\n<div class=\"rm-item\">\n<h5><a href=\"https:\/\/java2blog.com\/python-replace-space-with-underscore\/\" target=\"_blank\" rel=\"noopener\">Replace space with underscore in Python<\/a><\/h5>\n<div class=\"ex\">\n            <a href=\"https:\/\/java2blog.com\/python-replace-space-with-underscore\/\" target=\"_blank\" rel=\"noopener\">Read more \u2192<\/a>\n        <\/div>\n<\/p><\/div>\n<\/div>\n<\/section>\n<h2>Using the <code>re.subn()<\/code> function to replace word in string in Python<\/h2>\n<p>The <code>re.subn()<\/code> function works similarly to the previous function to replace word in string in Python. The difference lies in the format of the result.<\/p>\n<p>This function returns a tuple containing the new string and the number of matches found. It is used exactly like the <code>re.sub()<\/code> function.<\/p>\n<p>See the code below.<\/p>\n<pre code=\"python\" title=\"Using re.subn()\">\nimport re\ns1 = \"java 2 blog\"\ns2 = re.subn('java','python',s1)\nprint(s2)    \n<\/pre>\n<p>Output:<\/p>\n<div class=\"content-box-green\">\n(&#8216;python 2 blog&#8217;, 1)\n<\/div>\n<h2>Conclusion<\/h2>\n<p>In this article, we discussed several ways to replace word in string in Python. The first method uses the <code>replace()<\/code> function from the string class to replace word in string in Python. We use the <code>sub()<\/code> function from the <code>re<\/code> library in the second method that works with regular expressions. The <code>re.subn()<\/code> function was also discussed. It works the same as the previous method but returns output in a different structure. <\/p>\n<p>That&#8217;s all about how to replace word in String in Python.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, we will to how to replace word in string in Python. Strings represent a sequence of characters. In Python, they are immutable, which means that we cannot edit or modify strings. Whenever we perform any operation of modifying a string, we are creating a new copy. We will discuss how to replace [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[145,186],"tags":[],"_links":{"self":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/19848"}],"collection":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/comments?post=19848"}],"version-history":[{"count":0,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/19848\/revisions"}],"wp:attachment":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/media?parent=19848"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/categories?post=19848"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/tags?post=19848"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}