Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a List as 1 argument in replace()

Trying to solve.

I have a string from a user input. And I want to reomove all special characters from a list = [',', '.', '"', '\'', ':',]

using the replace function I´m able to remove one by one. using somethin like:

string = "a,bhc:kalaej jff!"
string.replace(",", "")

but I want to do remove all the special chr. in one go. I have tried:

unwanted_specialchr = [',', '.', '"', '\'', ':',]
string = "a,bhc:kalaej jff!"
string.replace(unwanted_specialchr, "")
like image 382
John_cabron Avatar asked Feb 06 '26 20:02

John_cabron


1 Answers

figured it out:

def remove_specialchr(string):
    unwanted_specialchr = [',', '.', '"', '\'', ':',]
    for chr in string:
        if chr in  unwanted_specialchr:
            string = string.replace(chr, '')
    return string
like image 134
John_cabron Avatar answered Feb 09 '26 08:02

John_cabron