DEV Community

Discussion on: Daily Challenge #276 - Unwanted Dollar Signs

Collapse
 
jpittiglio profile image
jpittiglio

This was great, but failed (Python 3.8) the first test due to the added space - added an additional replace and passed all tests.

def remove_dollar(string:str) -> float:
  remove_char = string.replace('$', '').replace(' ', '')
  return float(remove_char)
Collapse
 
laughinglove profile image
Josh

Thank you!

Collapse
 
rafaacioly profile image
Rafael Acioly • Edited

You could also use the method strip

return float(string.strip("$"))

strip doc:

S.strip([chars]) -> str

Return a copy of the string S with leading and trailing
whitespace removed.
If chars is given and not None, remove characters in chars instead.

Thread Thread
 
imjoseangel profile image
Jose Angel Munoz • Edited

This doesn't work with string = "-$ 0.1" isn't it? I'd rather prefer the replace way :P