Search code examples
pythonstrip

How can I remove dollar signs '$' from a list in Python?


sales = ['$1.21', '$2.29', '$14.52', '$6.13', '$24.36', '$33.85', '$1.92']

print(sales.strip('$'))

basically any scenario where you are given values with the $ sign and you need them gone, what is the best way to get rid of the dollar sign in python?


Solution

  • Strip from the left with str.lstrip():

    >>> sales = ['$1.21', '$2.29', '$14.52', '$6.13', '$24.36', '$33.85', '$1.92']
    >>> [s.lstrip("$") for s in sales]
    ['1.21', '2.29', '14.52', '6.13', '24.36', '33.85', '1.92']