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)

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