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!

7 posts were split to a new topic: List has not attribute: strip? (try a for loop)

12 posts were split to a new topic: How to add strings to a list?

A post was split to a new topic: What’s wrong with my code?

7 posts were split to a new topic: My list has every letter seperated instead of each line?

2 posts were split to a new topic: Print(’/n’) creates 2 lines?

A post was split to a new topic: Why does this repeat the task?

A post was merged into an existing topic: List has no attribute: strip? (try a for loop)

3 posts were merged into an existing topic: List has no attribute: strip? (try a for loop)

Why doesn’t this work?

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

How can I remove the ‘\n’ that appears on the list?

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

This is my code to strip every line and make multiline string, but I’m not sure on how to remove the ‘\n’ that appears on my list.

love_maybe_lines_stripped = \
[l.strip() for l in love_maybe_lines]

love_maybe_full = '\n'.join(love_maybe_lines_stripped)

print(love_maybe_full)

It, like other white space will be removed with l.strip(). We then put it back in on each break in the finished string so it still appears on multiple lines.

This method is not in-place so needs to be assigned or given as an argument in the list.append() method call.

1 Like

Thank you!, I understood

The ‘post the whole program’ message was a mistake of mine, I was trying to help someone in a question.

1 Like

Why is my answer for #2 incorrect when it prints the correct output?

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 = [x.strip() for x in love_maybe_lines if x != '\n']
print(love_maybe_lines_stripped)
print()
love_maybe_full = ' '.join(love_maybe_lines_stripped)
print(love_maybe_full)
print("Always in the middle of our bloodiest battles you lay down your arms like flowering mines to conquer me home.")

Last print is what view solution provided, and the print above it is my solution. They both match. So why is it flagging incorrect when the output matches?

I feel like whoever did the test for the String section are way to strict and did them to expect and exact result when in every programming language there are numerous ways to get the same result. Why does it all or nothing to pass this if you don’t type in

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 = []

for line in love_maybe_lines:
  love_maybe_lines_stripped.append(line.strip())
  
love_maybe_full = '\n'.join(love_maybe_lines_stripped)

print(love_maybe_full)

This to me is like telling a foreigner who can’t speak the dialect of English well that they are saying it wrong because you don’t like how they say the words, when in fact the words are the same. My code is doing the same thing. I am speaking a different dialect, but I am saying the same thing in the end result. So why are you flagging me for doing something wrong, when I am doing it right?

Is this a poor way to get to the solution for the first step?

new_list = []
i = 0
placeholder = ""

for element in love_maybe_lines:

    placeholder = love_maybe_lines[i]
    stripped_placeholder = placeholder.strip()
    new_list.append(stripped_placeholder)
    i += 1
    love_maybe_lines_stripped = new_list

Doesn’t look quite right. When you isolate and test this code, what result are you getting?

I am getting the right answer for the first step, but it’s fairly different from the solution. Adds more lines
of code.

I tend to approach these problems in my own way to get to the solutions. Should I focus more on the code provided to learn, or are those just there in case you’re unable to progress?

In some instances we will be required to follow instructions, in others the floor is open. If you comprehend a given solution well enough to explain everything it is doing, then by all means explore other ideas and possibilities. Just don’t let your confidence give you a false sense of security (correction bias) and be vigilante with testing.