What is a neat way to remove different characters from a string? For example, I have the following strings that I need to convert to integers:
($12,990)
$21,434
I use the following code which works fine, but is there a less bulkier way to do the same?
string = string.replace(",", "")
string = string.replace("$", "")
string = string.replace("(", "-")
string = string.replace(")", "")
int(string)
Edit: I am using Python 2.7.
string.replace("(", "-")a typo? That line doesn't remove a character...-were both a value to be replaced and a value to replace.str.translatehandles this nicely (and trivially). The question you marked as a duplicate does not seem to usestr.translateat all.str.translateis nice. This may suffice in this particular case.