{"id":19247,"date":"2022-02-08T16:44:33","date_gmt":"2022-02-08T11:14:33","guid":{"rendered":"https:\/\/java2blog.com\/?p=19247"},"modified":"2022-02-08T17:48:39","modified_gmt":"2022-02-08T12:18:39","slug":"remove-word-from-string-python","status":"publish","type":"post","link":"https:\/\/java2blog.com\/remove-word-from-string-python\/","title":{"rendered":"Remove Word from String in Python"},"content":{"rendered":"<div id=\"toc_container\" class=\"toc_light_blue no_bullets\"><p class=\"toc_title\">Table of Contents<\/p><ul class=\"toc_list\"><li><a href=\"#Remove_Word_from_String_in_Python\">Remove Word from String in Python<\/a><\/li><li><a href=\"#How_to_Remove_Word_from_Sentence_in_Python\">How to Remove Word from Sentence in Python<\/a><ul><li><a href=\"#Using_the_replace_function\">Using the replace() function<\/a><\/li><li><a href=\"#Using_the_resub_function\">Using the re.sub() function<\/a><\/li><li><a href=\"#Using_the_startswith_function\">Using the startswith() function<\/a><\/li><li><a href=\"#Using_the_removeprefix_function\">Using the removeprefix() function<\/a><\/li><li><a href=\"#Using_the_endswith_function\">Using the endswith() function<\/a><\/li><li><a href=\"#Using_the_removesuffix_function\">Using the removesuffix() function<\/a><\/li><\/ul><\/li><li><a href=\"#How_to_Remove_Duplicate_Words_from_String_in_Python\">How to Remove Duplicate Words from String in Python<\/a><ul><li><a href=\"#Using_the_set_function\">Using the set() function<\/a><\/li><li><a href=\"#Using_the_set_and_join_functions\">Using the set() and join() functions<\/a><\/li><li><a href=\"#Using_the_join_and_a_user-defined_function\">Using the join() and a user-defined function<\/a><\/li><li><a href=\"#Using_the_collectionsOrderedDict_class\">Using the collections.OrderedDict class<\/a><\/li><li><a href=\"#Using_the_numpyduplicate_function\">Using the numpy.duplicate() function<\/a><\/li><li><a href=\"#Using_the_regular_expressions\">Using the regular expressions<\/a><\/li><\/ul><\/li><li><a href=\"#Conclusion\">Conclusion<\/a><\/li><\/ul><\/div>\n<h2><span id=\"Remove_Word_from_String_in_Python\">Remove Word from String in Python<\/span><\/h2>\n<p>In this tutorial, different methods are demonstrated on how to remove word from string in Python. We will study two ways to achieve this. First, we will discuss how to remove a specific word from sentences in Python. Then, we will discuss how to remove duplicate words from a string in Python.<\/p>\n<h2><span id=\"How_to_Remove_Word_from_Sentence_in_Python\">How to Remove Word from Sentence in Python<\/span><\/h2>\n<p>We will first discuss methods to remove words from a string in Python.<\/p>\n<h3><span id=\"Using_the_replace_function\">Using the <code>replace()<\/code> function<\/span><\/h3>\n<p>We can use the <code>replace()<\/code> function to remove word from string in Python. This function replaces a given substring with the mentioned substring. We can replace a word with an empty character to remove it.<\/p>\n<p>For example,<\/p>\n<pre code = \"python\" title=\"Using replace()\">\na1 = \"remove word from this\"\na2 = a1.replace(\"word\", '')\nprint(a2)    \n<\/pre>\n<p>Output:<\/p>\n<div class=\"content-box-green\">\nremove  from this\n<\/div>\n<p>We can also specify how many occurrences of a word we want to replace in the function. For this, we can use the <code>count<\/code> parameter. By default, all occurrences are replaced.<\/p>\n<h3><span id=\"Using_the_resub_function\">Using the <code>re.sub()<\/code> function<\/span><\/h3>\n<p>The regular expressions can identify parts of a string using a pattern. The <code>re.sub()<\/code> function replaces a given substring that matches the regular expression pattern with some desired string.<\/p>\n<p>We can identify specific words using regular expressions and substitute them with an empty string to remove them.<\/p>\n<p>See the code below.<\/p>\n<pre code = \"python\" title=\"Using regex\">\nimport re\na1 = \"remove word from this\"\np = re.compile('(\\s*)word(\\s*)')\na2 = p.sub(' ', a1)\nprint(a2)    \n<\/pre>\n<p>Output:<\/p>\n<div class=\"content-box-green\">\nremove from this\n<\/div>\n<p>In the above example, the <code>re.compile()<\/code> function compiles a pattern that identifies the substring <code>word<\/code>. <\/p>\n<h3><span id=\"Using_the_startswith_function\">Using the <code>startswith()<\/code> function<\/span><\/h3>\n<p><strong>This method can remove word from the start of the sentence<\/strong>. The <code>startswith()<\/code> function returns True or False, based on whether the string starts with a given value or not. <\/p>\n<p>In this method, if the function returns True, we will slice the string till the length of the word to be removed.<\/p>\n<p>See the code below.<\/p>\n<pre code = \"python\" title=\"Using startswith()\">\na1 = \"word remove from this\"\na2 = a1[a1.startswith('word') and len('word'):]  \nprint(a2)\n<\/pre>\n<p>Output:<\/p>\n<div class=\"content-box-green\">\n remove from this\n<\/div>\n<h3><span id=\"Using_the_removeprefix_function\">Using the <code>removeprefix()<\/code> function<\/span><\/h3>\n<p>This is similar to the previous method. It will only remove words from the start of the sentence if they exist. This function only exists in Python 3.9 and above.<\/p>\n<p>For example,<\/p>\n<pre code = \"python\" title=\"Using removeprefix()\">\na1 = \"word remove from this\"\na2 = a1.removeprefix('word')  \nprint(a2)    \n<\/pre>\n<p>Output:<\/p>\n<div class=\"content-box-green\">\n remove from this\n<\/div>\n<h3><span id=\"Using_the_endswith_function\">Using the <code>endswith()<\/code> function<\/span><\/h3>\n<p><strong>This method can remove a word from the end of a sentence<\/strong>. The <code>endswith()<\/code> function returns True or False, based on whether the string ends with a given value or not.<\/p>\n<p>Here also, we will slice the string if the function returns True.<\/p>\n<p>See the code below.<\/p>\n<pre code = \"python\" title=\"Using endswith()\">\na1 = \"remove from this word\"\na2 = a1[:-(a1.endswith('word') and len('word'))]  \nprint(a2)    \n<\/pre>\n<p>Output:<\/p>\n<div class=\"content-box-green\">\nremove from this\n<\/div>\n<h3><span id=\"Using_the_removesuffix_function\">Using the <code>removesuffix()<\/code> function<\/span><\/h3>\n<p>This method is similar to the previous one and can eliminate a word from the end of the string. It is only available in Python 3.9 and above.<\/p>\n<p>For example,<\/p>\n<pre code = \"python\" title=\"Using removesuffix()\">\na1 = \"remove from this word\"\na2 = a1.removesuffix('word') \nprint(a2)    \n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"content-box-green\">\nremove from this\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\/python-remove-character-from-string\/\" target=\"_blank\" rel=\"noopener\">Remove character from String in Python<\/a><\/h5>\n<div class=\"ex\">\n            <a href=\"https:\/\/java2blog.com\/python-remove-character-from-string\/\" target=\"_blank\" rel=\"noopener\">Read more \u2192<\/a>\n        <\/div>\n<\/div>\n<div class=\"rm-item\">\n<h5><a href=\"https:\/\/java2blog.com\/remove-unicode-characters-python\/\" target=\"_blank\" rel=\"noopener\">Remove unicode Characters from String in Python<\/a><\/h5>\n<div class=\"ex\">\n            <a href=\"https:\/\/java2blog.com\/remove-unicode-characters-python\/\" target=\"_blank\" rel=\"noopener\">Read more \u2192<\/a>\n        <\/div>\n<\/p><\/div>\n<\/div>\n<\/section>\n<h2><span id=\"How_to_Remove_Duplicate_Words_from_String_in_Python\">How to Remove Duplicate Words from String in Python<\/span><\/h2>\n<p>We will now discuss how to remove duplicate words from string in Python. A common operation in these methods involves splitting a string into a list of words. For this, we use the <code>split()<\/code> function as shown in the sample codes.<\/p>\n<h3><span id=\"Using_the_set_function\">Using the <code>set()<\/code> function<\/span><\/h3>\n<p>A set is an unordered collection of elements. It contains only unique elements. We can use it to store a collection of unique words from a string. <\/p>\n<p>We can then use a <code>for<\/code> loop to compare each word and check whether it belongs in the set object or not. If the object is not present, it is appended to the final string.<\/p>\n<p>We implement this logic in the code below.<\/p>\n<pre code = \"python\" title=\"Using the set() function\">\na1 = \"remove word from word this word\"\ns = set()\na2 = ''\nfor word in a1.split():\n    if word not in s:\n        a2 = a2 + word + ' '\n        s.add(word)\nprint(a2)    \n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"content-box-green\">\nremove word from this\n<\/div>\n<p>In the above example, we can observe that we have successfully removed any duplicate words from the string <code>a1<\/code>.<\/p>\n<h3><span id=\"Using_the_set_and_join_functions\">Using the <code>set()<\/code> and <code>join()<\/code> functions<\/span><\/h3>\n<p>This method uses a similar approach to the previous method. We will proceed by splitting a string into a list of words. We will then pass this list to the <code>set()<\/code> function and automatically remove any duplicate words.<\/p>\n<p>After this, we will convert the words stored in the set object back to a string. For this, we will use the <code>join()<\/code> function. With the <code>join()<\/code> function, we can combine the elements of an iterable in a string by providing the separator character for the elements. <\/p>\n<p>Let us now use both these functions to remove duplicate words from a string in Python.<\/p>\n<p>See the code below.<\/p>\n<pre code = \"python\" title=\"Using the set() and join function\">\na1 = \"remove word from word this word\"\nl = a1.split()\na2 = ' '.join(sorted(set(l), key = l.index))\nprint(a2)    \n<\/pre>\n<p>Output:<\/p>\n<div class=\"content-box-green\">\nremove word from this\n<\/div>\n<p>In the above example, we use the <code>sorted()<\/code> function to maintain the order of the words in the string. We sort it by their index in the list <code>l<\/code>.<\/p>\n<h3><span id=\"Using_the_join_and_a_user-defined_function\">Using the <code>join()<\/code> and a user-defined function<\/span><\/h3>\n<p>This method also follows a similar approach to the previous one. We will start by splitting the string into a list of words. In this method, instead of using the sets to remove any duplicate, we will create a function that will eliminate duplicate words from the list.<\/p>\n<p>For example,<\/p>\n<pre code = \"python\" title=\"Using the join() and a user-defined function\">\ndef lst_unique(l):\n    lst = []\n    [lst.append(x) for x in l if x not in lst]\n    return lst\na1 = \"remove word from word this word\"\nl = a1.split()\na2 = ' '.join(lst_unique(l))\nprint(a2)    \n<\/pre>\n<p>Output:<\/p>\n<div class=\"content-box-green\">\nremove word from this\n<\/div>\n<p>In the above example, the <code>lst_unique()<\/code> function ensures that every element of the list is unique.<\/p>\n<h3><span id=\"Using_the_collectionsOrderedDict_class\">Using the <code>collections.OrderedDict<\/code> class<\/span><\/h3>\n<p>The <code>collections.OrderedDict<\/code> class creates a dictionary by arranging the order of the elements. We store the elements as keys and combine them using the <code>join()<\/code> function.<\/p>\n<p>For example,<\/p>\n<pre code = \"python\" title=\"Using the <code>collections.OrderedDict<\/code> class\">\nfrom collections import OrderedDict\na1 = \"remove word from word this word\"\nl = a1.split()\na2 = ' '.join(OrderedDict((s,s) for s in l).keys())\nprint(a2)    \n<\/pre>\n<p>Output:<\/p>\n<div class=\"content-box-green\">\nremove word from this\n<\/div>\n<h3><span id=\"Using_the_numpyduplicate_function\">Using the <code>numpy.duplicate()<\/code> function<\/span><\/h3>\n<p>The <code>numpy.duplicate()<\/code> function creates arrays from existing arrays, lists by eliminating the duplicate elements. We can use the list of words to create such an array of unique elements. After this, we will combine the elements using the <code>join()<\/code> function as done in the previous methods.<\/p>\n<p>The downside of this method is that it sorts the element, so the original order of the string is lost.<\/p>\n<p>See the code below.<\/p>\n<pre code = \"python\" title=\"Using the <code>numpy.duplicate()<\/code> function\">\nimport numpy as np\na1 = \"remove word from word this word\"\nl = a1.split()\narr = np.unique(l)\na2 = ' '.join(arr)\nprint(a2)    \n<\/pre>\n<p>Output:<\/p>\n<div class=\"content-box-green\">\nfrom remove this word\n<\/div>\n<h3><span id=\"Using_the_regular_expressions\">Using the regular expressions<\/span><\/h3>\n<p>We can use regular expressions to detect sub-strings based on regular expression patterns. We can use regular expressions to remove consecutive duplicate words using some pattern.<\/p>\n<p>We will use the <code>re.sub()<\/code> function to substitute the words that will match this pattern with the first occurrence of the word.<\/p>\n<p>See the code below.<\/p>\n<pre code = \"python\" title=\"Using regex\">\nimport re\na1 = \"remove word word word from this\"\na2 = re.sub(r'\\b(\\w+)( \\1\\b)+', r'\\1', a1)\nprint(a2)    \n<\/pre>\n<p>Output:<\/p>\n<div class=\"content-box-green\">\nremove word from this\n<\/div>\n<h2><span id=\"Conclusion\">Conclusion<\/span><\/h2>\n<p>This article demonstrated how to remove word from String in Python. Let&#8217;s wrap up with the most straightforward methods discussed. The <code>replace()<\/code> and <code>re.sub()<\/code> function can remove a specific word from a string very easily. Other methods remove words from the start or end of the sentence. We also discussed how to remove duplicate words from a string in Python. The main approach to remove duplicate words was to split the string into an iterable, remove the duplicate items, and combine them into a string again.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Table of ContentsRemove Word from String in PythonHow to Remove Word from Sentence in PythonUsing the replace() functionUsing the re.sub() functionUsing the startswith() functionUsing the removeprefix() functionUsing the endswith() functionUsing the removesuffix() functionHow to Remove Duplicate Words from String in PythonUsing the set() functionUsing the set() and join() functionsUsing the join() and a user-defined functionUsing [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":19266,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"_mi_skip_tracking":false},"categories":[186,145],"tags":[],"_links":{"self":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/19247"}],"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=19247"}],"version-history":[{"count":0,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/19247\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/media\/19266"}],"wp:attachment":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/media?parent=19247"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/categories?post=19247"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/tags?post=19247"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}