Search code examples
pythonliststrip

Stripping '$'-sign from sales list


Why am I unable to strip '$' in the first method of code however I am able to strip in the second method?

Method 1

sales = ['$1.21', '$7.29', '$12.52', '$5.13', '$20.39', '$30.82', '$1.85', '$17.98', '$17.41', '$28.59']

for element in sales:
 element.strip('$')

 print(element)

Method 2

sales = ['$1.21', '$7.29', '$12.52', '$5.13', '$20.39', '$30.82', '$1.85', '$17.98', '$17.41', '$28.59']

for element in sales:

 print(element.strip('$'))

Solution

  • str.strip method does not modify given string, instead it returns the result as it is specified in official documentation.

    Return a copy of the string with the leading and trailing characters removed.

    Since element.strip('$') returns result instead of modifying element this code does nothing.