0

I'm trying to get rid of the whitespace on this list however I am unable to. Anyone know where I am going wrong with my code?

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

Terminal: 

Always    
     in the middle of our bloodiest battles  
you lay down your arms
           like flowering mines    
   to conquer me home.
6
  • 3
    See the docs for .strip(). It will only remove the whitespace from the beginning and the end of the string. Try '\n'.join(map(str.strip, love_maybe_lines)). Commented Jan 12, 2021 at 0:58
  • ...so strip the elements of love_maybe_lines before you join. Commented Jan 12, 2021 at 0:59
  • "strip" only works at begin and end of a string, never in-between. You have to strip each list item before joining them. Commented Jan 12, 2021 at 1:00
  • I thought by '\n.join()' the list, they're on separate lines so the whitespace is on the beginning and the end now? Commented Jan 12, 2021 at 1:03
  • @Blenderinho "strip" doesn't care about lines, it only cares about the string as a whole. Commented Jan 12, 2021 at 1:06

1 Answer 1

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

love_maybe_lines = [item.strip() for item in love_maybe_lines]

That may help.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.