.strip() need help with the exercise!

Hi, the exercise I need help with is the .strip one: https://www.codecademy.com/courses/learn-python-3/lessons/string-methods/exercises/strip

Why does the following code not work?
for x in love_maybe_lines:
x.strip()
love_maybe_lines_stripped.append(x)

the solution seems to bit a bit different, but i dont understand why:

for line in love_maybe_lines:
love_maybe_lines_stripped.append(line.strip())

In your first line of code, x.strip doesn’t modify the actual list item in the array, so when you append x to love_maybe_lines, you’re merely appending the original item.

This is more obvious when you insert a print statement:

for x in love_maybe_lines:
  print(x) -> "Always    "
  print(x.strip()) -> "Always"
  print(x) -> "Always    "
  love_maybe_lines_stripped.append(x)

You create the correct value but it’s never used so it’s immediately trashed.

2 Likes

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.