'How to replace a character of a string if it is in a list in Python?

I need to check if there are any of the characters of a list of characters in a string and replace it with another character.

e.g.

characters = [" ", "-", "@"]
table="name-table"

I want the code to replace the "-" with "_" How can Ido that?

What I've tried already:

table = "name-table"
characters = [" ", "-", "@"]

if any(x in table for x in characters):
    table.replace(x, "_")

but it doesn't work



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source