Thread Shed: Step 14 print function

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 …

https://www.codecademy.com/courses/learn-python-3/projects/thread-shed

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.

Screen Shot 2023-01-16 at 6.31.38 PM

What did I do wrong?

You have

for sale in sales:
  sale.strip("$")

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?

And here is what I wanted to see and am glad it came out this way …

Yes.

Here’s an example:

old_string = "....I'm a string...." print("old string:", old_string) new_string = old_string.strip(".") print('new_string = old_string.strip(".")') print("new_string:", new_string) print("old string:", old_string)

Here, you see that old_string.strip(".") does not change what’s in old_string