FAQ: String Methods - .strip()

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

I don’t understand why we have to create a list and append values to it. I am getting same results with just a for loop. Even without doing the join method I am getting string on new lines. Is there something I am missing or not understanding?

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:
  new_list = (line.strip())
  print(new_list)

you’re setting new_list to be the last value of love_maybe_lines, that seems pretty weird to me, are you sure that’s specifically what you meant?

you have an empty list that you’re not doing anything with

new_list is also not a list, is it? looks like a string to me.

is your loop supposed to print something or build up some value? seems to be doing a bit of both at the moment, so one of those things could probably be removed depending on which of them you actually mean should happen

the exercise calls for final output to be a large multi line string. So I am getting that with a for loop without first making a list using append and then using join to make a multi line string (as shown in the solution).

Please ignore the empty list

shouldn’t have things that do nothing.

then you would for example be able to say what the length of that string is.

s = 'blah'
print(len(s))

but you don’t have such a string do you? you printed a couple of strings, but you never built a large one

1 Like

So, if you removed the things that did nothing:

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

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

the rest looks like they’re attempts to do something. they should either be gone, since they do nothing, or maybe something should happen, in which case they’d need to be fixed

Thanks I got it. I have individual stings but not one multi-line string.

I made a kind of correction on the poem cause it was missing some ajustments.

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

correction = []
part1 = []
part2 = []
love_maybe_full1 = []

correction = love_maybe_lines[1].split()

part1 = correction[0:3]
part2 = correction[3:]

part1_full = ' '.join(part1)
part2_full = ' '.join(part2)

for x in love_maybe_lines:
  love_maybe_lines_stripped.append(x.strip())

love_maybe_build.append(love_maybe_lines_stripped[0])
love_maybe_build.append(part1_full)
love_maybe_build.append(part2_full)
love_maybe_build.append(love_maybe_lines_stripped[2])
love_maybe_build.append(love_maybe_lines_stripped[3])
love_maybe_build.append(love_maybe_lines_stripped[4])
love_maybe_build.append(love_maybe_lines_stripped[5])

love_maybe_full = '\n'.join(love_maybe_build)
  
print(love_maybe_full)

Now is the same line indentation of the original poem. :slightly_smiling_face:

1 Like

Hey guys just struggling a bit right now because my code seems to be giving me what I need for the first part but its not giving me a tick, any idea why this might be?

love_maybe_lines_stripped = []
def stripping():
  for line in love_maybe_lines:
   love_maybe_lines_stripped.append(line.strip(' '))
    
stripping()

print(love_maybe_lines_stripped)

Is this an experiment or what the exercise asks for? It might be that the lesson checker is looking for inline code. The function may be unexpected.

There is an extra line between the last two lines as a result of the \n in the original list. When I use the following code I get the following result. Is this the expected result? Is my code correct?

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 = [line.strip() for line in love_maybe_lines]

love_maybe_full = '\n'.join(love_maybe_lines_stripped)
print(love_maybe_full)

image

2 Likes

Yes I believe that was intentional. A few sources online have the same formatting for those lines.

In lesson 8.

I tried to use join first to concatenate all the pieces from love_maybe_lines, then I used strip. Why doesn’t .strip clear the spaces accordingly?

lines = " ".join(love_maybe_lines)

print(lines)

love_maybe_full = lines.strip()

print(love_maybe_full)

Strip without arguments removes whitespace at either end of a string, not within a string.

test = "     A big   red  cat      !"
print(test.strip())
Out: A big   red  cat      !
# Strip only acts on the beginning and end of the string.

You check the method in the docs-

Thanks. Appreciated!

Thank you! Huge help!

Why in the case of the .strip string method does it require a list?

Why with .join or .split it doesn’t?

For example,
if I run print(‘delimiter’.join(list_old))
or
list_new = ‘delimiter’.join(list_old)
It works fine…

If I run print(list_old.split(delimiter))
or
list_new = list_old.split(delimiter)
It also works fine…

But if I try it with strip -
print(list_old.strip())
or
list_new = list_strip.old()

it errors every time… I’ve seen the answers on how to get this to work, so I’m no much looking for the resolution, so much as the logic as to why .strip doesn’t behave the same way as .join or .split?

It doesn’t. The method lets us remove specified characters from beginning or end of a string.

str. strip([chars])

If you are referring to the above syntax, the square brackets are not a list literal, but symbolic of a sequence of characters.

>>> s = 'abcdeliveriesbca'
>>> print (s.strip('abc'))
deliveries
>>> 
1 Like