FAQ: String Methods - .strip()


#1

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!


#2

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.


#3

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?


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

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


#5

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

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

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

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


#6

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:
    love_maybe_lines_stripped.append(line.strip())

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.


#7

love=""
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?


#8

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.


#9

Hello!
Doing the exercise, I wrote this code:

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

Greetings


#10

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.


#11

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:
love_maybe_lines_stripped.append(line.strip())

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.


#12

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

+= [line.strip()]

#13

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.


#14

Now I understand.
Thank you so much!


#15

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


#17

Why does
print("\n")
print two lines worth of space and not one as I anticipated it would?

print(“x”)
print("\n")
print(“y”)

results in:

x
(empty line of space)
(empty line of space)
y

and not:

x
(empty line of space)
y


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

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


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.