Remove words from string python

Looking at the other answers to your question I noticed that they told you how to do what you are trying to do, but they did not answer the question you posed at the end.

If the input query is "What is Hello", I get the output as:

wht s llo

Why does this happen?

This happens because .replace[] replaces the substring you give it exactly.

for example:

"My, my! Hello my friendly mystery".replace["my", ""]

gives:

>>> "My, ! Hello  friendly stery"

.replace[] is essentially splitting the string by the substring given as the first parameter and joining it back together with the second parameter.

"hello".replace["he", "je"]

is logically similar to:

"je".join["hello".split["he"]]

If you were still wanting to use .replace to remove whole words you might think adding a space before and after would be enough, but this leaves out words at the beginning and end of the string as well as punctuated versions of the substring.

"My, my! hello my friendly mystery".replace[" my ", " "]
>>> "My, my! hello friendly mystery"

"My, my! hello my friendly mystery".replace[" my", ""]
>>> "My,! hello friendlystery"

"My, my! hello my friendly mystery".replace["my ", ""]
>>> "My, my! hello friendly mystery"

Additionally, adding spaces before and after will not catch duplicates as it has already processed the first sub-string and will ignore it in favor of continuing on:

"hello my my friend".replace[" my ", " "]
>>> "hello my friend"

For these reasons your accepted answer by Robby Cornelissen is the recommended way to do what you are wanting.

This article is created to cover some programs in Python, that removes a particular word [entered by user] from a string [entered by user]. Here are the list of approaches used to do the task:

  • Remove a Word from String using replace[]
  • Using List

Remove a Word from String using replace[]

To remove or delete a desired word from a given sentence or string in Python, you have to ask from the user to enter the string and then ask to enter the word present in the string to delete all the occurrence of that word from the string and print the new string like shown in the program given below:

print["Enter the String: "]
text = input[]

print["Enter a Word to Delete: "]
word = input[]

text = text.replace[word, ""]

print[]
print[text]

Here is the initial output produced by this Python program:

Now supply the input say welcome to codescracker as string, then to as word to delete. Here is the sample output with exactly same input:

From above program, the following code [statement]:

text = text.replace[word, ""]

states that the value of word gets replaced with "" [nothing] in given string stored in text. That is, in place of every word's value, nothing [""] gets placed. In this way, the word gets deleted or removed

Modified Version of Previous Program

The end used in this program, to skip inserting an automatic newline using print[]. The + is used to concatenate string. And the \" is used to print " on output:

print["Enter String: ", end=""]
text = input[]

print["Enter a Word to Delete: ", end=""]
word = input[]

wordlist = text.split[]
if word in wordlist:
    text = text.replace[word, "\b"]
    print["\nNew String without \"" +word+ "\":"]
    print[text]
else:
    print["\n\"" +word+ "\" is not found in the string!"]

Here is its sample run with string input as this is python programming, then word input as python:

Here is another sample run with same string input, but word input as codescracker:

Note - The split[] method splits the string into words.

For example, if the string stored in text are this is python programming, then after executing the following code:

the list named wordlist is created with its values [elements] as:

['this', 'is', 'python', 'programming']

Note - The "\b" is used to insert backspace on output. This escape character is used to shift upcoming thing from here to one space back.

Remove Word from String using List

This program does the same job, that is to remove word from a string, but using list. Let's have a look at the program first:

print["Enter String: ", end=""]
text = input[]
print["Enter a Word to Delete: ", end=""]
word = input[]

wordlist = text.split[]
newtext = [x for x in wordlist if x not in word]

print["\nNew String is:"]
for x in newtext:
    print[x, end=" "]
print[]

Here is its sample run with user input, welcome to codescracker to learn Python as string and to as word to delete:

From above program, the following statement:

newtext = [x for x in wordlist if x not in word]

is the short form of following block of code:

newtext = []
for x in wordlist:
    if x not in word:
        newtext.append[x]

Modified Version of Previous Program:

This is the modified version of previous program. The join[] method in this program used in a way that, the list newtext is converted into a string:

print["Enter String: ", end=""]
text = input[]
print["Enter a Word to Delete: ", end=""]
word = input[]

wordlist = text.split[]
if word in wordlist:
    newtext = [x for x in wordlist if x not in word]
    newtext = ' '.join[newtext]
    print["\nNew string after removing \"" +[word]+ "\":"]
    print[newtext]
else:
    print["\n\"" +[word]+ "\" is not found in the string!"]

Here is its sample run with same user input as of previous program's sample run:

Same Program in Other Languages

  • Java Remove Word from Sentence
  • C Remove Word from Sentence
  • C++ Remove Word from Sentence

Python Online Test

« Previous Program Next Program »

How do I remove multiple words from a string in Python?

Use str. replace[] to remove multiple characters from a string.

How do I remove a specific word from a list in Python?

How do I remove a specific element from a list in Python? The remove[] method removes the first matching element [which is passed as an argument] from the list. The pop[] method removes an element at a given index, and will also return the removed item.

How do you remove part of a string in Python?

Remove a part of a string in Python.
Remove a substring by replacing it with an empty string. ... .
Remove leading and trailing characters: strip[].
Remove leading characters: lstrip[].
Remove trailing characters: rstrip[].
Remove prefix: removeprefix[] [Python 3.9 or later].
Remove suffix: removesuffix[] [Python 3.9 or later].

How do I remove a specific word from a string?

Given a string and a word that task to remove the word from the string if it exists otherwise return -1 as an output..
Convert the string into a string array..
Iterate the array and check the word not equal to the given word..
Concatenate the word into a new string array name as a new string..
Print the new string..

Chủ Đề