codescracker


python

Python Program to Remove Word from Sentence



« Previous Program Next Program »


Remove Word from Sentence in Python

To remove or delete the occurrence of a given 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 sentence and finally print the string without that word as shown in the program given below.

Python Programming Code to Remove Word from Sentence

Following python program ask from the user to enter a string and a word to remove the word from the string and show/print the string without that word:

# Python Program - Remove Word from Sentence

print("Enter 'x' for exit.");
string = input("Enter any string to remove particular word: ");
if string == 'x':
    exit();
else:
    word = input("Enter word to be delete/remove: ");
    print("\nDeleting given word from the given string...");
    print("New String after successfully deleting",word);
    word_list = string.split();
    print(' '.join([i for i in word_list if i not in word]));

Here is the sample run of the above python program to illustrate how to remove word from sentence or string:

Below one is the inital output asking from user to enter any string to delete any particular word from it as shown here:

remove word from string python

If you supply the given string say this is what codescracker is and press enter key, then the program will ask to enter the word say is which you want to delete from the given string and then press enter key to start performing the deletion operation of the given word that is is from the string and then see the same string but without deleted word as shown here in below sample run:

delete word form string python

Same program on python shell:

python remove word from sentence

Same Program in Other Languages

You may also like to learn or practice the same program in other popular programming languages:


« Previous Program Next Program »








Tools
Calculator

Quick Links
Signup - Login - Give Online Test