Hatch
Hatch

Reputation: 11

Removing Characters in a String with Characters from a List

def replace_chars(stringa, listb):
    '''A function to return stringa by removing the list of characters from listb'''
    #print(stringa)
    #print(listb)
    for x in stringa:
        if x == listb:
            stringa.replace(x,listb)
    return stringa
if __name__ == "__main__":
    import test
    test.testEqual(replace_chars(" My!Name*Is#John*",["!", "*", "#", "*"]), " My Name Is John ")

Basically, I'm supposed to have the characters from the list be taken out of the string like in the test. I'm unsure why this isn't working any help would be greatly appreciated. I need it as a function like this so I can import it into other files.

Thank you for your time

Upvotes: 0

Views: 40

Answers (1)

Akhil Thayyil
Akhil Thayyil

Reputation: 9403

Try :

def replace_chars(stringa, listb):
    '''A function to return stringa by removing the list of characters from listb'''
    #print(stringa)
    #print(listb)
    for x in listb:
        stringa = stringa.replace(x,' ')
    return stringa
if __name__ == "__main__":
    #import test
    print(replace_chars(" My!Name*Is#John*",["!", "*", "#", "*"]))

Upvotes: 1

Related Questions