FAQ: String Methods - .strip()


This community-built FAQ covers the “.strip()” exercise from the lesson “String Methods”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Computer Science

FAQs on the exercise .strip()

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!


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.


Can somebody explain why the code below prints out " to conquer me home. " instead of every line?

for line in love_maybe_lines:

Yet, when the code below adds every line to the empty list.

love_maybe_lines_stripped =
for line in love_maybe_lines:


Your first example does not append anything to a list, just prints the unchanged line. strip() is not an insitu operation but one that needs to be redirected to a variable or a print statement.

for line in love_maybe_lines:
    print (line.strip())

line, itself is not modified. Only the printed value is stripped.

We’re not instructed to modify the original list, only to create a spinoff list of modified strings.

for line in love_maybe_lines:

as the solution has shown. In either event, we had to redirect (assign) the outcome to some other object or process. APPEND assigns it to a list; PRINT passes it as an argument to a process (in Python 3 it is a function). The act of stripping has no effect upon the object being stripped.


for lines in love_maybe_lines:
love+=’ '+lines.strip()
print (love)

I get the right output, but it doesn’t say it’s correct… what to do?


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.


Doing the exercise, I wrote this code:

Which I don’t understand because it gives me back that result, letter by letter.



Did you expect something else? Have you thought through what you expected? That may tell you a lot about what to change. Even if you don’t know how to make it happen, deciding what should happen is a very good start and arguably required since you, the programmer, is the one deciding how information should be moving around in your program (that’s not something to be left to chance)

If you’ve got each stripped line as individual strings, and you then end up with a list of individual characters with no linefeeds, then I think that is good enough information to conclude that the issue is with how those strings were added to the list - what list operation did you use, what did you ask the list to do? Maybe list makes some other operation(s) available to you that suit you better.

1 Like

Thank you very much for your reply.
I perfectly understand what you are saying. But my question went to the most technical.
I’m using a blucle for and taking each string from the list and removing the spaces with .sprit().
What I hoped would happen is that I would return the entire string without the spaces removed, just as the next for loop would execute:

for line in love_maybe_lines:

What I don’t understand is why you don’t do the same with (+=).
Maybe my question is very silly, but I’m just beginning and it would be useful to explain why I don’t get the same result in the loop when I use (+=) and not .append().
It is very possible that I misunderstood the concept of (+=) and misapplied it.


When concatenating to a list, both objects must be lists.

+= [line.strip()]

What do you have to support that += should behave like append?
Right? It does something, and it’s not going to do what you want, it’s going to do whatever it was defined to do.

One reason why += shouldn’t behave like you want it to is that + doesn’t behave like that:

[] + 5  # uhm, what?

Instead, list’s + expects another list:

[] + []  # still empty, but this makes sense at least

You might then expect += to raise some error when the other value isn’t a list, same as + would. However, it iterates over the other value, it doesn’t care at all what type it is so long as it is iterable. And if you iterate over a string you get individual characters.

1 Like

Now I understand.
Thank you so much!

1 Like

I didn’t know that.
Your contributions helped me a lot
Thank you so much!


Why does
print two lines worth of space and not one as I anticipated it would?


results in:

(empty line of space)
(empty line of space)

and not:

(empty line of space)

>>> print('x');print();print('y')

>>> print('x');print('\n');print('y')


print() by itself takes a separate line. We can conclude that Python is inserting a newline at the end, so printing one means there are now two.

1 Like

‘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.


yeah this exercise is not pedagogical enough for beginners like me, and its not just this exercise. many others too

1 Like