DEV Community

Discussion on: Daily Challenge #276 - Unwanted Dollar Signs

Collapse
 
laughinglove profile image
Josh • Edited

This is my solution in Python3

def remove_dollar(string: str) -> float:
  remove_char = string.replace('$', '' ).replace(' ', '')
  return float(remove_char)

EDIT: Removed space, thanks for correcting me guys

Collapse
 
aredhi profile image
Ardi

you alse need to remove space from the string to avoid the exception

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