FAQ: String Methods - .strip()

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?

1 Like

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.

Focus on the actions and data types involved, ignore code while you decide what should happen, and once you’ve decided use code to describe that.
Every single part of the code has to be doing something that you decided should happen. If it doesn’t, then it shouldn’t be there. Don’t write something that does something different from what you decided unless you also decide that the plan should change.

Actually that’s entirely fair. If you get a single character wrong, even one you can’t see, yes, it’s absolutely wrong.

You’d be right if you got exactly the same string in the end and it still wouldn’t pass.
And you’d be right if you did do exactly as described, but the exercise expected something else.

The special treatment of != '\n' isn’t right, because you’re looking for a specific string instead of “extra whitespace” (too specific, tailored to the specific input). Also, it shouldn’t be excluded. It should be stripped of whitespace and what’s left should be included, because the instructions state that each thing in the original list is a line.

Hey everyone I would like to demonstrate how I solved the first part of the problem.

So for the first part I used a list comprehension. ** >>> [EXPRESSION for ITEM in LIST] **

The EXPRESSION part would be the .strip()
The ITEM part would be the temporary variable we assign in the LIST we have been given in this case which is ‘love_maybe_lines’.

So the full list comprehension would be:

love_maybe_lines_stripped = [x.strip() for x in love_maybe_lines]

What you’re doing here is that you are stripping the temporary variable you assign in love_maybe_lines.

Using a list comprehension will save you from writing long lines of code sometimes.

1 Like