List has no attribute: strip? (try a for loop)

Im starting to get frustrated over the way that you learn from codecademy. For example in this excercise it didn´t say in the instructions that you think about iterating/forloop (or whats it called) to be able to strip the list. The example was not even close to prepare you for that.


As said by jansson173, i didn’t expect the answer to be in for loop for the first part.
I wrote my code simply like this:

love_maybe_lines_stripped = love_maybe_lines.strip()

How is that this code above is wrong? How is that using a for loop in this exercise suits well?

>>> love_maybe_lines.strip()
Traceback (most recent call last):
  File "<pyshell#13>", line 1, in <module>
AttributeError: 'list' object has no attribute 'strip'

We need to parse out the individual lines which as strings DO have a strip() method.

1 Like

Agreed. When looking on stackoverflow everyone was recommending to use .replace() instead. Since that was not the objective of this exercise there was no useful information on how to properly .strip() a string. Their “example” was not an example, just a console output of what the string would look like of implemented correctly. Thinking there was a bug with the exercise or something small I was not missing I clicked “solution” to find that it was a “for” loop with the rest of the lesson completed. Since I am new to coding I find this to be irritating because my logic doesn’t exactly turn to breaking outside the box if in at least it is not suggested or “hinted”. If there was just a “hint”: “maybe think about a for loop…” or whatever, this would be better instruction rather than put up some bogus information and expecting new programmers to improvise off it.

Sorry about the rant. Just frustrated.


‘list’ object has no attribute ‘strip’

  • Sorry, I can"t seem to grasp what the sentence above really means. What does it mean exactly?

strip is an attribute (method) of class str objects. It’s purpose is to remove leading and trailing characters. If no character(s) are given as an argument, the default is whitespace.

We cannot strip a list.

1 Like

Hey surendazz, I believe it means that I can not strip the list :).

i think the confusing part is that most of the time the instructions guide you as what you have to do and in this one to use for is not evident, maybe if at any part of the instructions they have used the words iterate, or for each one, etc, implying you have to for each element on a list, and not just strip…

1 Like

Is there a way to solve this exercise using list comprehensions? And if so, how?

Essentially, write the loop inside list brackets…

b = [x.strip() for x in a]

I’m considering switching over to another learning site, it just doesn’t worth my money and time to get exercises where I have to use things I just haven’t learned yet.
how do you expect people to resolve this if at this point they haven’t seen anything about loops, list or the use of .append?
it’s just ridiculous!


hello my friend. actually we have been through lists AND loops. just look at the python 3 syllabus. number 4 is lists and number 5 is loops. i don’t have a strong memory but im pretty sure that append should be in the Lists section.

Happy coding!!

i really know how you feel …
but i think it’s a good way to learn coding by this way … you just need to think every time before solving your problem … so here, codecademy told u that .strip() is working with strings not lists , so you need to iterate every element in the list (not the whole list) with for loop or any other loops , because every element is a string and .strip() is working with strings .


Couldn’t agree more!

I get the feeling I’m close with this code, for the first part of this exercise, but I get the dreaded " Value for love_maybe_lines_stripped did not match the expected value." error. Can anyone tell me why this doesn’t work?

I really want to stop looking at the answers and try to figure this out myself (with a little help from you fine people) :slight_smile:

love_maybe_lines = ['Always    ', '     in the middle of our bloodiest battles  ', 'you lay down your arms', '           like flowering mines    ','\n' ,'   to conquer me home.    ']

love_maybe_lines_stripped = []
def strip_the_poem(love_maybe_lines):
  for line in love_maybe_lines:

what happens when you return instead of print?

I get the same error. Not sure why, as “return” was my first instinct, too.

Let’s write a generic function…

def strip_the_poem(lines):
  y = []
  for line in lines:
  return y

We call it with,


Notice that y is defined inside the function.

Okay, what you’re saying makes sense. Now I did this, and it’s saying " Could not find love_maybe_lines_stripped ." which I really don’t understand:

def strip_the_poem(love_maybe_lines):
  love_maybe_lines_stripped = []
  for line in love_maybe_lines:
  return love_maybe_lines_stripped

I’m not actually calling the function yet, because there is one more small part to the exercise after this, and I don’t think we’re supposed to call it yet.

The call will come, if even only on testing. Still, consider the generic approach that uses symbols in place of verbose names. I can debug my code; can you debug yours?