I’m assuming my question has a simple solution (but serious that I can’t get a code to work)! I’m working on the Learn Python 3 - Strings - Thread Shed project. See link below …
I’ve happily made it down to step 14. The first part of step 14 is to simply remove the “$” character. I coded it, and wanted to print out my code to see that the output really has the “$” stripped. See my code below …
Should be so simple and no problem right? But the output looks like the screenshot below … I’ve tried adding the return command with different indentations as well as the print command with different indentations. But the output still has the “$” character.
Here, sale.strip("$") returns a new string, it does not change the original one. (Strings are immutable in Python … meaning that they can’t be changed, they can only be replaced).
sale = sale.strip("$") won’t work either here (due to how Python does iteration, I think).
You can iterate using the index.
for i in range(len(sales)):
sales[i] = sales[i].strip('$')
A different alternative is to change sales.append(data[1])
to sales.append(data[1].strip("$"))
Thank you janbazant for your reply. Your examples for iteration and for appending confused me even further. Your thinking is way more advanced than mine! What is helping me to think of this a different way is when you said that the .strip(“$”) code returns a new string, not changing the original string. Ok. So why is it when I tried to print this new string, the original string with the original format still popped up?
Continuing to explore what you meant by “sale.strip(”$“) returns a new string, it does not change the original one.” So I experimented with creating a new list (see below) that will print out what I am curious to see (a list of the sales without the “$” symbol). It worked. And I guess I answered my own question without fully understanding why it had to be this way? Just accept it as it is?