Arshad Mehmood

Search the blog

    Today I Learned

    Replace multiple terms from a string

    python

    Replacing multiple terms in a string.

    def replaceMultiple(mainString, toBeReplaces, newString):
        # Iterate over the strings to be replaced
        for elem in toBeReplaces:
            # Check if string is in the main string
            if elem in mainString:
                # Replace the string
                mainString = mainString.replace(elem, newString)
    
        return mainString
    
    wordsToRemove = ['via', 'test', 'good']
    text = replaceMultiple(originalText, wordsToRemove, '')

    Got it from here