How do i remove multiple words from a string in python?

bannedWord = ["Good", "Bad", "Ugly"]
    
def RemoveBannedWords[toPrint, database]:
    statement = toPrint
    for x in range[0, len[database]]:
        if bannedWord[x] in statement:
            statement = statement.replace[bannedWord[x] + " ", ""]
    return statement
        
toPrint = "Hello Ugly Guy, Good To See You."
    
print[RemoveBannedWords[toPrint, bannedWord]]

The output is Hello Guy, To See You. Knowing Python I feel like there is a better way to implement changing several words in a string. I searched up some similar solutions using dictionaries but it didn't seem to fit this situation.

Paul P

2,5192 gold badges7 silver badges22 bronze badges

asked Jul 7, 2015 at 15:56

I use

bannedWord = ['Good','Bad','Ugly']
toPrint = 'Hello Ugly Guy, Good To See You.'
print[' '.join[i for i in toPrint.split[] if i not in bannedWord]]

answered Jul 7, 2015 at 16:01

ShreevardhanShreevardhan

11.7k3 gold badges36 silver badges47 bronze badges

0

Here's a solution with regex:

import re
    
def RemoveBannedWords[toPrint,database]:
    statement = toPrint
    pattern = re.compile["\\b[Good|Bad|Ugly]\\W", re.I]
    return pattern.sub["", toPrint]
    
toPrint = "Hello Ugly Guy, Good To See You."
    
print[RemoveBannedWords[toPrint,bannedWord]]

Paul P

2,5192 gold badges7 silver badges22 bronze badges

answered Jul 7, 2015 at 16:03

Ajay GuptaAjay Gupta

1,2807 silver badges22 bronze badges

0

Slight variation on Ajay's code, when one of the string is a substring of other in the bannedWord list

bannedWord = ['good', 'bad', 'good guy' 'ugly']

The result of toPrint ='good winter good guy' would be

RemoveBannedWords[toPrint,database = bannedWord] = 'winter good'

as it will remove good first. A sorting is required wrt length of elements in the list.

import re

def RemoveBannedWords[toPrint,database]:
    statement = toPrint
    database_1 = sorted[list[database], key=len]
    pattern = re.compile[r"\b[" + "|".join[database_1] + "]\\W", re.I]
    return pattern.sub["", toPrint + ' '][:-1] #added because it skipped last word

toPrint = 'good winter good guy.'

print[RemoveBannedWords[toPrint,bannedWord]]

answered Oct 4, 2017 at 14:06

ItachiItachi

2,51323 silver badges32 bronze badges

Yet another variation on a theme. If you are going to be calling this a lot, then it is best to compile the regex once to improve the speed:

import re

bannedWord = ['Good', 'Bad', 'Ugly']
re_banned_words = re.compile[r"\b[" + "|".join[bannedWord] + "]\\W", re.I]

def RemoveBannedWords[toPrint]:
    global re_banned_words
    return re_banned_words.sub["", toPrint]

toPrint = 'Hello Ugly Guy, Good To See You.'
print[RemoveBannedWords[toPrint]]

answered Jul 7, 2015 at 16:18

Martin EvansMartin Evans

44.1k16 gold badges80 silver badges91 bronze badges

1

As you're checking for the word boundary in the beginning and a non word character at the end, regex is preferable. Still in-memory array/list can also be used

bannedWord = ['Good', 'Bad', 'Ugly']

toPrint = 'Hello Uglyyy Guy, Good To See You.'

for word in bannedWord:
    toPrint = toPrint.replace[word, ""]

print[toPrint] 
Hello yy Guy,  To See You.

[Program finished] 

answered Feb 25, 2021 at 6:47

SubhamSubham

3231 gold badge5 silver badges13 bronze badges

How do you delete multiple words in Python?

Use str. replace[] to remove multiple characters from a string. Create a copy of the original string. Put the multiple characters that will be removed in one string.

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

To remove multiple characters from a string we can easily use the function str. replace and pass a parameter multiple characters. The String class [Str] provides a method to replace[old_str, new_str] to replace the sub-strings in a string. It replaces all the elements of the old sub-string with the new sub-string.

How do I remove words from a string in Python?

text = input['Enter a string: '] words = text. split[] data = input['Enter a word to delete: '] status = False for word in words: if word == data: words. remove[word] status = True if status: text = ' '. join[words] print['String after deletion:',text] else: print['Word not present in string.

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].

Chủ Đề